Log in or register to post comments

MarkerID - What changed?

August 15, 2013 - 10:26am #1

Hi there, this relates to a problem I was having here:

https://developer.vuforia.com/forum/unity-3-extension-technical-discussion/markermarkerid-issue-always-returns-nullreferenceexcept

But since upgrading to Unity 4.2 and Vuforia 2.6.7 I'm getting a: NullReferenceException: Object reference not set to an instance of an object:

at this line:

 

Debug.Log("Trackable " + mMarkerBehaviour.Marker.MarkerID + " lost");

 

Everything is setup just like before:


		mMarkerBehaviour = GetComponent<MarkerBehaviour>();
		
		if (mMarkerBehaviour==null)
        {
           Debug.Log ("Marker Behaviour NOT found!");
        }
	

 

of the OnTrackingLost method.

I'm manually adding the markers:

MarkerTracker markerTracker = (MarkerTracker)TrackerManager.Instance.GetTracker(Tracker.Type.MARKER_TRACKER);
					MarkerBehaviour mb = markerTracker.CreateMarker(_poi.MarkerNumber, "marker" + _poi.MarkerNumber, 100);
					if (mb){ 
						mb.gameObject.AddComponent<TurnOffBehaviour>();
						mb.gameObject.AddComponent<TrackableEventHandlerPOI>();
						//StateManagerImpl stateManager = (StateManagerImpl)TrackerManager.Instance.GetStateManager();
						//stateManager.AssociateMarkerBehaviours();
						
						
						Debug.Log(mb.gameObject.GetComponent<TrackableEventHandlerPOI>().name + " added to tracker" + mb.name);
						

And I've tried with and without the //stateManager... hotfix that was recommended in the previous thread.  No difference in behaviour.  I've confirmed that the Markers are being added prior to the TrackableEventHandlerPOI script in the script execution order.

So my question is: what changed?  When you mentioned this problem was "fixed", am I still looking for the markerID correctly?  The MarkerBehaviour and Marker scripts don't look any different...

I'd appreciate any insight.

MarkerID - What changed?

August 16, 2013 - 6:37am #7

Ah, who needs sleep anyway?  ;)

MarkerID - What changed?

August 15, 2013 - 6:16pm #6

We refactored the API, just to keep everyone on their toes ;)

MarkerID - What changed?

August 15, 2013 - 5:35pm #5

It turns out it's because I was deriving from an older (v2.30) DefaultTrackableEvenHandler that didn't cast MarkerBehaviour (posted below).  It was missing the following line:

MarkerBehaviour markerBehaviour = (MarkerBehaviour) mTrackableBehaviour;

So that's my mistake for not catching that change to the DefaultTrackableEventHandler.  I've just been starting at too many screens these days.

 

/*==============================================================================
            Copyright (c) 2012 QUALCOMM Austria Research Center GmbH.
            All Rights Reserved.
            Qualcomm Confidential and Proprietary
==============================================================================*/

using UnityEngine;

// A custom handler that implements the ITrackableEventHandler interface.
public class DefaultTrackableEventHandler : MonoBehaviour,
                                            ITrackableEventHandler
{
    #region PRIVATE_MEMBER_VARIABLES
 
    private TrackableBehaviour mTrackableBehaviour;
    
    #endregion // PRIVATE_MEMBER_VARIABLES



    #region UNTIY_MONOBEHAVIOUR_METHODS
    
    void Start()
    {
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
        {
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }

        OnTrackingLost();
    }

    #endregion // UNTIY_MONOBEHAVIOUR_METHODS



    #region PUBLIC_METHODS

    // Implementation of the ITrackableEventHandler function called when the
    // tracking state changes.
    public void OnTrackableStateChanged(
                                    TrackableBehaviour.Status previousStatus,
                                    TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED)
        {
            OnTrackingFound();
        }
        else
        {
            OnTrackingLost();
        }
    }

    #endregion // PUBLIC_METHODS



    #region PRIVATE_METHODS


    private void OnTrackingFound()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();

        // Enable rendering:
        foreach (Renderer component in rendererComponents) {
            component.enabled = true;
        }

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    }


    private void OnTrackingLost()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();

        // Disable rendering:
        foreach (Renderer component in rendererComponents) {
            component.enabled = false;
        }

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    }

    #endregion // PRIVATE_METHODS
}

 

MarkerID - What changed?

August 15, 2013 - 3:29pm #4

Actually I'm not sure of why you're getting a NRE, because I'd tested the MarkerBehaviour component directly using the following and am able to get IDs for both authored and runtime generated markers.

/*==============================================================================
Copyright (c) 2010-2013 QUALCOMM Austria Research Center GmbH.
All Rights Reserved.
Confidential and Proprietary - QUALCOMM Austria Research Center GmbH.
==============================================================================*/

using UnityEngine;

/// <summary>
/// A custom handler that implements the ITrackableEventHandler interface.
/// </summary>
public class DefaultTrackableEventHandler : MonoBehaviour,
                                            ITrackableEventHandler
{
    #region PRIVATE_MEMBER_VARIABLES
 
    private TrackableBehaviour mTrackableBehaviour;
	private MarkerBehaviour mMarkerBehaviour;
    
    #endregion // PRIVATE_MEMBER_VARIABLES



    #region UNTIY_MONOBEHAVIOUR_METHODS
    
    void Start()
    {
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
		mMarkerBehaviour = GetComponent<MarkerBehaviour>();
		
        if (mTrackableBehaviour)
        {
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }
    }

    #endregion // UNTIY_MONOBEHAVIOUR_METHODS



    #region PUBLIC_METHODS

    /// <summary>
    /// Implementation of the ITrackableEventHandler function called when the
    /// tracking state changes.
    /// </summary>
    public void OnTrackableStateChanged(
                                    TrackableBehaviour.Status previousStatus,
                                    TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED)
        {
            OnTrackingFound();
        }
        else
        {
            OnTrackingLost();
        }
    }

    #endregion // PUBLIC_METHODS



    #region PRIVATE_METHODS


    private void OnTrackingFound()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
        Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

        // Enable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = true;
        }

        // Enable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = true;
        }
		
		MarkerBehaviour markerBehaviour = (MarkerBehaviour) mTrackableBehaviour;
		Debug.Log( markerBehaviour.Marker.MarkerID + " found");
		
		Debug.Log("Component ID = "+ mMarkerBehaviour.Marker.MarkerID );
        //Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    }


    private void OnTrackingLost()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
        Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

        // Disable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = false;
        }

        // Disable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = false;
        }
		
		MarkerBehaviour markerBehaviour = (MarkerBehaviour) mTrackableBehaviour;
		Debug.Log( markerBehaviour.Marker.MarkerID + " Lost");
		
		Debug.Log("Component ID = "+ mMarkerBehaviour.Marker.MarkerID );
        //Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    }

    #endregion // PRIVATE_METHODS
}

 

MarkerID - What changed?

August 15, 2013 - 2:14pm #3

Aha!  That is a bit different.

 

MarkerBehaviour mMarkerBehaviour = (MarkerBehaviour) mTrackableBehaviour;

The line above did it.  Sorry, I didn't realize the new way to do it was to cast the TrackableBehaviour instance to a MarkerBehaviour locally. I suppose I could have also changed:

 mMarkerBehaviour = GetComponent<MarkerBehaviour>();
        
        if (mMarkerBehaviour==null)
        {
           Debug.Log ("Marker Behaviour NOT found!");
        }

to:

MarkerBehaviour mMarkerBehaviour = (MarkerBehaviour) mTrackableBehaviour;

globally but either way it seems to be working now.  Hope this helps someone else in future too.

Thanks again!

MarkerID - What changed?

August 15, 2013 - 1:22pm #2

Does

		MarkerBehaviour markerBehaviour = (MarkerBehaviour) mTrackableBehaviour;
		Debug.Log( markerBehaviour.Marker.MarkerID + " found");

work for you within the DefaultTrackableEventHandler?

 

Also here is a script for creating and destroying Frame Markers that you can use for comparison..

 

/*============================================================================
Copyright (c) 2012-2013 QUALCOMM Austria Research Center GmbH.
All Rights Reserved.
Confidential and Proprietary - QUALCOMM Austria Research Center GmbH.
============================================================================*/

using UnityEngine;
using System.Collections;

public class MarkerController : MonoBehaviour
{
    private int mNextMarkerID = 0;

    void OnGUI()
    {
        MarkerTracker markerTracker = (MarkerTracker)TrackerManager.Instance.GetTracker(Tracker.Type.MARKER_TRACKER);

        if (GUI.Button(new Rect(20, 20, 150, 50), "Create Marker"))
        {
            MarkerBehaviour mb = markerTracker.CreateMarker(mNextMarkerID, "marker" + mNextMarkerID, 100);
            if (mb != null)
            {
                mb.gameObject.AddComponent<DefaultTrackableEventHandler>();
                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                cube.transform.parent = mb.transform;
                cube.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
                cube.transform.localPosition = new Vector3(0.0f, 0.25f, 0.0f);
                mNextMarkerID++;
            }
        }

        if (GUI.Button(new Rect(20, 90, 150, 50), "Destroy Last Marker"))
        {
            foreach(Marker marker in markerTracker.GetMarkers())
            {
                if (marker.MarkerID == mNextMarkerID - 1)
                {
                    markerTracker.Stop();
                    if (markerTracker.DestroyMarker(marker, true))
                    {
                        mNextMarkerID--;
                    }
                    markerTracker.Start();
                    break;
                }
            }
        }

        if (GUI.Button(new Rect(20, 160, 150, 50), "Destroy All Markers"))
        {
            markerTracker.DestroyAllMarkers(true);
            mNextMarkerID = 0;
        }
    }
}

 

Log in or register to post comments