Search Unity

Start/Stop Vuforia when dealing with multiple scenes

Discussion in 'Vuforia' started by Red_Dragon69, Dec 13, 2018.

  1. Red_Dragon69

    Red_Dragon69

    Joined:
    Sep 7, 2015
    Posts:
    62
    Hy, this should just be an informal post that costed several days to figure out. Just to save others from wasting their time, I'll post my problem + solution here. Maybe some lucky dev will stumble upon this and find the help he needs, when it comes to deal with the extremly annoying error

    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.


    My scenario is some kind of lobby where I can acces different scenes with different modes (including multiple scenes for tracking targets -> lets call them "tracking scenes"). To save performance I only init/activate/deactivate Vuforia tracking, when entering a tracking scene. My main camera is not destroyed on scene load, but everytime I exited and entered a tracking scene, Vuforia will complain with above target invocation exception. I wrote a short script for every tracking scene, to reassociate the targets when starting. BÄM, easy as that.

    If someone from Vuforia staff reads this, can you pls provide additional information at your documentation for this? It's very hard to find such things, without proper documentation.

    Code (CSharp):
    1. void Start() {
    2.     if (VuforiaRuntime.Instance.InitializationState == VuforiaRuntime.InitState.INITIALIZED) {
    3.         TrackerManager.Instance.GetStateManager().ReassociateTrackables();
    4.         VuforiaBehaviour.Instance.enabled = true;
    5.         return;
    6.     }
    7. }
    Best regards :)
     
  2. LLTorres

    LLTorres

    Joined:
    Aug 4, 2019
    Posts:
    1
    I have the same issue, but when put your script on some game object, vuforias crash and not start. Can you help me?
     
  3. Red_Dragon69

    Red_Dragon69

    Joined:
    Sep 7, 2015
    Posts:
    62
    Depending on the scene architecture, this solution may or may not work. In my case the script is only called in the so called "tracking scenes". That means that Vuforia is already up and running when entering the "lobby scene". Make sure VuforiaBehaviour is on your MainCamera and it's not destroyed on load. So everything should be working before loading a new scene and reassociate new targets.