MixedRealityToolkit-Unity icon indicating copy to clipboard operation
MixedRealityToolkit-Unity copied to clipboard

Instance isn't found if class is on a disabled GameObject.

Open timGerken opened this issue 9 years ago • 8 comments

https://github.com/Microsoft/MixedRealityToolkit-Unity/blob/62491815d88337f8cf98060c1d45e6d19554eae1/Assets/HoloToolkit/Utilities/Scripts/SingleInstance.cs#L21 If the derived class is attached to an object that starts out as disabled, its Instance won't be found because the method used here doesn't find disabled objects. changing to use Resources.FindObjectsOfTypeAll<T>(); resolves the issue.

timGerken avatar Sep 27 '17 18:09 timGerken

The fix is unfortunately incomplete. Works great at runtime, but not so great in playmode in the editor where it returns prefabs as well. Need to figure out how to filter out prefabs.

timGerken avatar Sep 27 '17 23:09 timGerken

Not sure if this should be a bug or not. It makes sense that a disabled game object wouldn't be running.

StephenHodgson avatar Oct 02 '17 18:10 StephenHodgson

if Object.FindObjectsOfTypeAll(Type type) weren't marked as 'obsolete', I'd recommend using that.

I still think of this is a bug.

Another alternative fix might be to allow for there to be Zero instances found in the scene and return null in that case and only spew to the debugger if there are > 1. That way I can write code like this:

    if (ClassICareAbout.Instance != null &&
        ClassICareAbout.Instance.PropertyICareAbout)
    {
        // do something_
    }

timGerken avatar Oct 02 '17 18:10 timGerken

I think the biggest part of the problem is that we're attaching instances to GameObjects in the first place.

Ideally these instances should live on their own and not be tied to scene object references, but alas this is Unity, and in order to have a script run it needs to be a Component that you've got to attach to a GameObject that has to be enabled in your scene.

It's a split in the traditional paradigm of how software engineers usually treat programming, and is usually one of the biggest gripes traditional software engineers have with using Unity.

StephenHodgson avatar Oct 02 '17 18:10 StephenHodgson

Additionally, ._Instance is set the first time that .Instance finds the <T> it is looking for. It will continue to return that value, even if the GameObject it is attached to is later disabled until OnDestroy(), at which time ._Instance is set to null. Since you don't have any control over the order in which OnDestroy() is called, you still need to do the null check as above.

timGerken avatar Oct 02 '17 19:10 timGerken

If your concern is the debug spew when there are zero objects, we could wrap the warning about not finding an instance in an ifdef.

darax avatar Oct 11 '17 20:10 darax

The spew for having >1 still seems valid, though.

timGerken avatar Oct 11 '17 21:10 timGerken

Going to remove the bug label on this. Doesn't really fit because a Singleton based on Monobehaviour will never be found.

StephenHodgson avatar Oct 12 '17 00:10 StephenHodgson