• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Nels83 · Oct 23, 2015 at 11:57 AM · unity 5instantiatevrrandomsingleton

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)

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image smallbit · Oct 28, 2015 at 09:16 AM 0
Share

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.

avatar image Nels83 smallbit · Oct 29, 2015 at 02:18 AM 0
Share

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.

avatar image smallbit Nels83 · Oct 29, 2015 at 02:26 AM 0
Share

You can use magnet pull using dive. I didn't have any problems with it, they provide how to do it in the docs.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nels83 · Oct 29, 2015 at 02:31 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving a object randomly without it being inside a wall 1 Answer

Instantiated Object Wont save a reference to the prefab, instead sets to itself 1 Answer

Array values changing after gameObject is instantiated from prefab 1 Answer

How to walk in the direction the player is looking at in the Vive headset 1 Answer

How to Instantiate only once? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges