MixedRealityToolkit-Unity: Getting intermittent null ref on SetGlobalListener
Using the new MixedRealityCameraParent prefab, InputManager, etc. with updated code (both on master and 2017.2.0 versions) I’m getting an intermittent null ref exception on
InputManager.Instance.AddGlobalListener(gameObject)
NullReferenceException: Object reference not set to an instance of an object
HoloToolkit.Unity.InputModule.SetGlobalListener.OnEnable () (at Assets/HoloToolkit/Input/Scripts/Utilities/SetGlobalListener.cs:16)
Specifically, the instance variable is null.
In the current version of the script, OnDisable and OnDestroy wrap the RemoveGlobalListener call in an if statement
private void OnDisable()
{
if (InputManager.Instance != null)
{
InputManager.Instance.RemoveGlobalListener(gameObject);
}
}
But OnEnable does not:
private void OnEnable()
{
InputManager.Instance.AddGlobalListener(gameObject);
}
Presumably, this should be
private void OnEnable()
{
if (InputManager.Instance != null)
{
InputManager.Instance.AddGlobalListener(gameObject);
}
}
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 22 (10 by maintainers)
OK. I’ve also mostly used single scenes, but for larger projects I’m not sure how well this scales – maybe just an unwarranted assumption on my part from looking at other Unity projects. Perhaps I’m also abusing Singletons in my project.
It might be helpful to have an project in the HoloToolkit-Examples that uses multiple scenes so that cross scene issues can be detected.
One additional question about this is the hard dependency between the MixedRealityCameraParent’s SetGlobalListener script and the InputManager. Because of the lack of a null check on the OnEnable function, if you don’t have an InputManager in the scene – for example if you have a short-lived splash scene that doesn’t allow interactivity – it will throw an error.
Maybe this is necessary as other assets in the MixedRealityCameraParent, such as MixedRealityTeleport, depend on it, but if so the dependency should be make explicit (maybe it is, haven’t looked at the documentation recently). Otherwise, seems like they could be refactored to allow for separate inclusion in a scene.
I have a working theory that seems to explain the behavior (sorry, behaviour) and the inconsistency.
The two objects interacting are the MixedRealityCameraParent gameobject and attached script SetGlobalListener and the InputManager gameobject with the attached InputManager script inheriting from Singleton.
Awake is effectively random as to when it is called. But, so is OnDestroy. OnEnable can have dependencies as to when called. Static values are preserved across scene loads.
Two scenes each with a MixedRealityCameraParent and an InputManager. When the transition between the scenes is called, the order in which the two objects are destroyed is important.
If the camera is destroyed first the SetGlobalListener OnDestroy checks for null on the InputManager.Instance.
Because this is not yet null, the InputManager.Instance.RemoveGlobalListener function is called. Next the InputManager is destroyed and the Singleton static values are reset and ready for the new scene. All is OK.
If the InputManager is destroyed first, the gameobject is destroyed and the Singleton static values reset and ready for the new scene. Now the camera is destroyed. SetGlobalListener calls its OnDestroy method which checks
Now though, the Instance is null and searchForInstance has been reset to true. So in the Singleton Instance property, we hit this:
This resets searchForInstance to false. But, there is nothing to return from FindObjectsOfType<T> as the InputManager has already been destroyed. The new scene loads but the static value of searchForInstance survives the scene transition.
Now, if the InputManager loads before the MixedRealityCameraParent, the InputManager Singleton Awake method works properly, reassigns the Singleton Instance property, the searchForInstance value is still false but that is now correct, and all is again OK.
However, if the MixedRealiityCameraParent loads first, then the SetGlobalListener OnEnable gets called, but because the static searchForInstance is still false and the instance is still null, accessing InputManager.Instance throws the nullreference exception.
After this the InputManager Awake is called and you still get a functioning inputmanager. Though I think things are still in a bad state as the MixedRealityCameraParent is never added to the AddGlobalListener call.
This seems to mactch what I’m seeing, especially when I include the timing of the SetGlobalListener OnEnable, OnDisable and OnDestroy functions.
I really appreciate you taking the time to test this out and explore the problem a little deeper.
Hmm, I’m pretty sure if they don’t need to override then base
OnDestroyit should be called by default.The above code is redundant, and shouldn’t require the need to override it, but I could be wrong.
Depending on where you put your breakpoint and if the instance was the the same, you may or may not have hit it because it was a different instance.
I’ll test this out.
OK this test has an OnDestroy override in InputManager to make sure the Singleton base destroy method is called, although its presence doesn’t materially impact the errors.
The timing of Destroy and Awake seems correct. Interestingly the InputManager, even after the error thrown in GlobalListener, does eventually get started up, the cursor works etecetera. Also, the errors seem to compound the more scenes that get loaded, especially if I move through them rapidly. I can also mitigate some issues by using SceneManager.LoadSceneAsync and waiting for the load.
Again, all of this is inconsistent and seems to change a bit from one run to another.
SCENE 0 InputManager Singleton static searchForInstance is: True InputManager Awake is called: IM #1 InputManager Start is called: IM #1 Exiting scene 0
SCENE 1 InputManager OnDestroy is called: IM #1
Multiple EventSystems in scene… this is not supported
InputManager Singleton static searchForInstance is: True NullReferenceException: Object reference not set to an instance of an object
InputManager Awake is called: IM #2 InputManager Start is called: IM #2 Exiting scene 1
InputManager OnDestory is called: IM #2
@Alexees I have a pretty good handle on managing the loading within a scene. The trouble I am having is between scenes with the InputManager and the new Singleton class.
It seems to be related to the persistence of static variables between scenes (c.f. link above, this one http://answers.unity3d.com/questions/1169580/static-variable-reset-at-loadlevel.html, et.al.), as well as timing, but it is maddeningly inconsistent.
If I throw a bunch of debug statements, I can see the chain:
The searchForInstance value is true so the first scene’s InputManager.Instance is created. InputManager Singleton static searchForInstance is: True HoloToolkit.Unity.InputModule.SetGlobalListener:OnEnable()
My code to change scenes runs Exiting scene 0 GameManagers.SceneLevelManager:LoadNextScene()
The new scene loads and now the old InputManager is still around with its EventSystem as well as the new scene’s InputManager and EventSystem, throwing the warning below Multiple EventSystems in scene… this is not supported UnityEngine.EventSystems.EventSystem:OnEnable()
The old InputManager is removed, but the static class variable is still set, so the new InputManager doesn’t have the Instance assigned InputManager Singleton static searchForInstance is: False HoloToolkit.Unity.InputModule.SetGlobalListener:OnEnable()
And the exception below is thrown NullReferenceException: Object reference not set to an instance of an object HoloToolkit.Unity.InputModule.SetGlobalListener.OnEnable () (at Assets/HoloToolkit/Input/Scripts/Utilities/SetGlobalListener.cs:17)
The problem is typical for Singletons that initialize themselves in Awake (as this one does). The call cycle is Awake and then immediately OnEnable is called if the script is enabled. Since Awake calls are arbitrary, but OnEnable is tied to it, there’s no way to tell if the singleton is ready by the time another OnEnable is called. There are two solutions to this. Either move the Singleton in question up the Execution order table, or do the singleton lookup always in Start (which is guaranteed to be called after every Awake, hence every OnEnable, has been executed). If you need to toggle your script. just skip the first OnEnable and use Start once for the first initialization.
The latest installment of the toolkit yields until the singleton is available, so you can also check that out.
This suggests that the issue is caused from the static class references in Singleton.cs which survive scene load
http://answers.unity3d.com/questions/41891/static-variables-between-scenes.html