Log in or register to post comments

OnTrackingLost gets called before Start on Unity

July 18, 2013 - 6:48am #6

Vuforia SDK Version: 2.5.8 -

Description with steps to reproduce:

Try the following code with Vuforia 2.0.32; works. Then with 2.5.8; it doesn't.

public class SomeTrackable : MonoBehaviour, ITrackableEventHandler
{
    private TrackableBehaviour mTrackableBehaviour;
    GameObject ChildName;

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

        ChildName= transform.FindChild("SomeChildName").gameObject;
    }

    public void OnTrackableStateChanged(
                                    TrackableBehaviour.Status previousStatus,
                                    TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED)
        {
            OnTrackingFound();
        }
        else
        {
            OnTrackingLost();
        }
    }

    void OnTrackingFound()
    {
    }

    void OnTrackingLost()
    {
        ChildName.SetActive(true);
    }
}

 

-

Development OS (Mac OS X, Windows, Linux): Mac OS X Lion, Windows 7 -

Mobile OS and Version: Any -

Mobile Device Manufacturer and Model name: Any -

Do the Vuforia Sample Applications show the same behavior?: Didn't test

OnTrackingLost gets called before Start on Unity

July 18, 2013 - 7:16am #5

Hi, thank you for this report;

what do you exactly mean with:

with Vuforia 2.0.32; works. Then with 2.5.8; it doesn't.

 

I tested in both versions with the Image Targets sample (by replacing the DefaultTrackableEventHandler with your code), and I seem to get exactly the same behaviour ...

What is the behaviour that you would expect ?

OnTrackingLost gets called before Start on Unity

August 1, 2013 - 5:36am #4

Sorry for the delay and thanks for your reply.

What I have is a NullReferenceException because ChildName hasn't been initialized.

OnTrackingLost gets called before Start on Unity

August 1, 2013 - 9:49am #3

ChildName hasn't been initialized.

Well, you need to find out why this is, most likely using the Unity debugger.

Saying it was working in a previous version is not really the issue, as it is almost definitely to do with your code.

Please check what gets called first with the debugger, because Start() must be getting called first.  If not it is a Unity issue.

 

N

OnTrackingLost gets called before Start on Unity

August 5, 2013 - 7:39am #2

What I was trying to report was a change of behaviour between Vuforia 2.5.8 and its previous version. This bug of mine isn't really the point here, because changing the order of initialization made my code work.

I had working code. Then updated Vuforia. Then had that initialization error.

If it doesn't affect anyone else, please close this.

Thanks again,

Martín from Innovar Group.

OnTrackingLost gets called before Start on Unity

August 6, 2013 - 2:35am #1

Hi Martin,

I checked with the development team and this behaviour is possible and is working as expected.

Essentially, QCAR will call back with a lost event on startup for every known target (in order to hide it initially).

The order of the Start() methods is not defined, so it’s possible that QCAR is fully initialized when the Start() method in your code is invoked. In such a case, the OnTrackableStateChanged will be immediately called when the EventHandler is registered. Since ChildName is set AFTER registering the event handler, it will be null and an exception will be thrown.
 
The solution is to set the ChildName before registering the TrackableEventHandler.  i.e.
 
    void Start()
    {
        ChildName= transform.FindChild("SomeChildName").gameObject;
 
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
        {
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }
 
    }

 

HTH

N

Log in or register to post comments