Kinect with mc-sdk usage between scenes

I’m in the process of fixing some one else’s code for a museum installation. It’s a program with two scenes. both have an empty gameObject that handles important cross scene stuff like the Kinect manager. it has a DontDestroyOnLoad verification function to ensure that the object isn’t instantiated twice:

    private void InstantiateController() {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(this);
        }
        else if (this != Instance)
        {
            Destroy(this.gameObject);
        }
    }

and this seems to be working, the object is being transfered between scenes and some of script methods are working correctly. the problem is with the kinect initialisation:

void Update () {

        KinectManager manager = KinectManager.Instance;
      
        if (manager && manager.IsInitialized ()) {
            if (manager.IsUserDetected ()) {
                uint userId = manager.GetPlayer1ID ();
                if (manager.IsJointTracked (userId, (int) joint)) {
                  // stuff hapenning ...
                }
            } 
        } 
    }

the kinect related stuff works fine on the first scene but as soon as the second scene loads up and this is moved to the DontDestroyOnLoad, the manager == null but manager.isInitialized() is still true;

I know this is probably a terrible way to do things in unity. I rarely use unity myself. not looking for a best solution or a better approach, i’m really just looking for a fix (or dirty hack).

How is that possible? If manager is null, manager.IsInitialized doesn’t even get called due to lazy evaluation. Are you getting an error somewhere? If yes, paste it here so we can take a closer look.

no errors, nothing. it’s weird indeed just printing those two things give me those outputs. I’ve been at this for a week now and tried just about everything.

You’re saying that if you load another scene and place Debug Logs on 2 separate lines like this:

Debug.Log(manager == null);
Debug.Log(manager.IsInitialized());

Both of these print true?

Im not sure if this is related to your actual problem, but Unity made a big mistake that they can’t fix anymore (could cause many breaking changes) by overloading the == check for null, someGameObject == null will return true if the GameObject is destroyed, even if the actual C# object is still “alive” and not null.

You can find many topics about this question/problem, but if you want a true null check then you have to cast your GameObject to object before the null check or use object.ReferenceEquals(yourGameObject, null)

Some examples: Is it possible to perform a TRUE null-check? - Questions & Answers - Unity Discussions

yes, but if that’s to weird, ignore it for now. It’s the manager that returns null after the new scene is loaded, even though it’s on a dontDestroyOnLoad object. I put a print in the awake function of KinectManager itself and it does fire again when loading a new scene, but KinectManager also has a DontDestroyOnLoad in the awake itself?

yes, these:

on first scene returns:

but after scene change, with a dont destroy on load returns:

I tried casting Instance to an object, same result:

    private void InstantiateController() {
        if ((object)Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(this);
        }
        else if (this != Instance)
        {
            Destroy(this.gameObject);
        }
    }