Google Cardboard singleton is not respectful of my wishes....

Hello!

I’ve been having an odd issue with the Cardboard SDK (0.5.1 and 0.5.2) in Unity 5.2.1f1

The project I have consists of 1 scene, with the CarboardMain Prefab (with no distortion) which can be reloaded (using keyboard input in Editor, and magnet pull in builds to call Applcation.LoadLevel(0) ).

ISSUE: (happens in Editor and in builds)
When the scene starts, I sometimes get this message in the Console:

Cardboard SDK object should be a
singleton.
UnityEngine.Debug:LogWarning(Object)
Cardboard:Awake() (at
Assets/Cardboard/Scripts/Cardboard.cs:472)

And in the Scene Hierarchy I can see the CardboardMain prefab and another gameObject called Cardboard with all the default settings, and in the game view I see the game with distortion on.

When I reload the scene from within the game, the same will sometimes happen and I get these messages in the console:

Some objects were not cleaned up when
closing the scene. (Did you spawn new
GameObjects from OnDestroy?)

Creating Cardboard object
UnityEngine.Debug:Log(Object)
Cardboard:get_SDK() (at
Assets/Cardboard/Scripts/Cardboard.cs:40)
CardBoardControls:OnDisable() (at
Assets/CardBoardControls.cs:12)

As I keep reloading, sometimes the new Cardboard gameObject will vanish (and my view is back to my settings with no distortion) and other times it will spawn yet another Cardboard gameObject.

When I stop the game, that new Cardboard gameObject will sometimes still be in my Hierarchy.

What I find really strange with this, is that it is random…happens about 50% of the time when I reload the scene, and maybe 10% of the time when I first start the scene.

I have Googled and searched through forums for the last couple of nights but haven’t come across any posts with this issue. Has anyone come across this issue and solved it?

thanks in advance!
-Nelson

**EDIT:
I still have not solved this issue, but I do have a work around that works for me since I only have one scene:
I commented out the second if statement of public static Cardboard SDK in the Cardboard.cs script:

public class Cardboard : MonoBehaviour {
  /// The singleton instance of the Cardboard class.
  /// Not null: the instance is created automatically on demand if not already present.
  public static Cardboard SDK {
    get {
      if (sdk == null) {
        sdk = UnityEngine.Object.FindObjectOfType<Cardboard>();
      }
//      if (sdk == null) {
//        Debug.Log("Creating Cardboard object");
//        var go = new GameObject("Cardboard");
//        sdk = go.AddComponent<Cardboard>();
//        go.transform.localPosition = Vector3.zero;
//      }
      return sdk;
    }
  }

NOTE: I do get this error in the console now when I restart the scene, but it doesn’t affect my game:

NullReferenceException: Object
reference not set to an instance of an
object CardBoardControls.OnDisable ()
(at Assets/CardBoardControls.cs:11)

I’d recommend to not mess with code from SDKs, otherwise you’d need to keep track of your changes in case of an Update. For your problem:

CardBoardControls seems to access the singleton OnDisable which one should avoid. the randomness comes from the unpredictable order of OnDisable executions when ending play. Sometimes CardBoardControls comes first and everything is fine, sometimes not, then the singleton is already destroyed but due to the pattern recreated on scene exit. Either save yourself a reference to the singleton and check for null on OnDisable, or remove the accessing code from OnDisable.