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:
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:
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.
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)
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?