Instance isn't found if class is on a disabled GameObject.
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.
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.
Not sure if this should be a bug or not. It makes sense that a disabled game object wouldn't be running.
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_
}
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.
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.
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.
The spew for having >1 still seems valid, though.
Going to remove the bug label on this. Doesn't really fit because a Singleton based on Monobehaviour will never be found.