DontDestroyOnLoad ARCamera 'deletes' ImageTargetBehaviour
I've recently updated my project from Vuforia 5.5 to 6.2 using the migration guide found here: https://library.vuforia.com/articles/Solution/Migrating-Unity-Projects-to-Vuforia-6-2
I am working on Unity 5.5
I originally had a scene with an ARCamera with Keep Camera Alive ticked so it was one camera throughout the whole of the app.
With 6.2 there's not an option for that so I put a script on the Camera saying 'DontDestroyOnLoad(this);' in the Start function.
It then automatically goes into the new AR scene which includes ImageTargets with 3D models etc augmenting.
This is where the problem occurs; the screen freezes and the console creates hundreds of duplicate errors saying:
Exception in callback: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> UnityEngine.MissingReferenceException: The object of type 'ImageTargetBehaviour' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
at (wrapper managed-to-native) UnityEngine.Behaviour:get_enabled ()
at Vuforia.StateManagerImpl.UpdateTrackablePoses (UnityEngine.Transform arCameraTransform, .TrackableResultData[] trackableResultDataArray, .VuMarkTargetResultData[] vuMarkResultDataArray, TrackableIdPair originTrackableID, Int32 frameIndex) [0x00000] in <filename unknown>:0
at Vuforia.VuforiaManagerImpl.UpdateTrackers (FrameState frameState) [0x00000] in <filename unknown>:0
at Vuforia.VuforiaManagerImpl.Update (ScreenOrientation counterRotation, Boolean reapplyOldState) [0x00000] in <filename unknown>:0
at Vuforia.VuforiaARController.UpdateStatePrivate (Boolean forceUpdate, Boolean reapplyOldState) [0x00000] in <filename unknown>:0
at Vuforia.VuforiaARController.UpdateState (Boolean forceUpdate, Boolean reapplyOldState) [0x00000] in <filename unknown>:0
at Vuforia.DigitalEyewearARController.Update () [0x00000] in <filename unknown>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x000d0] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222
--- End of inner exception stack trace ---
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x000eb] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232
at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115
at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x000b9] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Delegate.cs:443
at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00018] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/MulticastDelegate.cs:71
at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Delegate.cs:415
at Vuforia.DelegateHelper.InvokeDelegate (System.Delegate action, System.Object[] args) [0x00000] in <filename unknown>:0
UnityEngine.Debug:LogError(Object)
Vuforia.DelegateHelper:InvokeDelegate(Delegate, Object[])
Vuforia.DelegateHelper:InvokeWithExceptionHandling(Action)
Vuforia.VuforiaAbstractBehaviour:Update()
Within the project (in play mode) I can see that nothing has been deleted, ImageTargetBehaviour is on all (5) Image Targets and everything looks like it works.
I feel like this could be an issue with the camera missing something when it moves scenes perhaps?
Can anyone help please?
Answer by hollym16 · Mar 23, 2017 at 05:00 PM
For anyone who has the same issue, I found the fix and feel quite stupid for not seeing it before. On the migration guide (found here: https://library.vuforia.com/articles/Solution/Migrating-Unity-Projects-to-Vuforia-6-2) there's a bit at the bottom about KeepAliveBehaviour. Just paste the following line of script into the DefaultTrackableEventHandler:
TrackerManager.Instance.GetStateManager().ReassociateTrackables();
Tada!