- Home /
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 cannot help you with the issue, but I have had many problems with google plugin myself, after i finally made it compile It was crashing on the device, in the end I moved to Dive SD$$anonymous$$ and all my problems disappeared.
I have used Dive SD$$anonymous$$ before, and I also found it $$anonymous$$UCH easier to use. I decided to try the Carboard SD$$anonymous$$ because of the trigger pull and the screen distortion correction.
You can use magnet pull using dive. I didn't have any problems with it, they provide how to do it in the docs.
Answer by hexagonius · Oct 28, 2015 at 07:08 AM
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.
hexagonius, you are correct about the CardBoardControls.OnDisable()! I commented out that function in my CardBoardControls.cs script, and reverted back to the original Cardboard.cs. The scene runs fine now every time.
Thank you!!
For anyone curious about what the CardBoardControls.cs script was doing:
public class CardBoardControls : $$anonymous$$onoBehaviour {
void OnEnable(){
Cardboard.SD$$anonymous$$.OnTrigger += TriggerPulled;
}
void OnDisable(){
Cardboard.SD$$anonymous$$.OnTrigger -= TriggerPulled;
}
void TriggerPulled() {
if(this.gameObject.GetComponent<Game$$anonymous$$anager>().isIntro){
this.gameObject.GetComponent<intro$$anonymous$$anger>().active = false;
}else{
Application.LoadLevel(0);
}
}
}
Your answer
Follow this Question
Related Questions
Instantiated Object Wont save a reference to the prefab, instead sets to itself 1 Answer
Moving a object randomly without it being inside a wall 1 Answer
Random instantiate position doesn't work 1 Answer
How to Spawn after checking if the clones are destroyed. 1 Answer
Daydream publishing errors 1 Answer