Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by r005t · Jun 04, 2014 at 04:13 PM · coroutinestaticinstancesingleton

Singleton instance accessed in coroutine always null

I have two singleton objects: one which managers the user data, and another which manages Goal/mission data. In order to keep the code organized, I wanted to handle loading saved data, etc through the User script. However, whenever the user instance accesses the GoalDatabase script in it's Awake, the latter's static instance is always seen as null.

I set the user class up as follows:

 public class User:MonoBehaviour
 {
     public static User userInstance;
 
     void Awake()
     {
 
         //setup instance if this is the only copy
         if (userInstance == null) 
         {
             /*Setup this instance*/
             DontDestroyOnLoad (this.gameObject);
             userInstance = this;
 
             /*Load the saved user data*/
             bool ReturningUser = this.LoadData();
 
             /*Load or import goal data*/
             StartCoroutine("SetGoalData",ReturningUser);
 
         } 
         else if (userInstance != this) 
         {
             Destroy(this.gameObject);
         }
     }
 
 
 public IEnumerator SetGoalData(bool returningUser)
     {
         Debug.Log ("In SetGoalData coRoutine and ReturningUser:" + returningUser.ToString());
 
         //wait for Goal Database object to instantiate
         while (GoalDatabase._instance == null) 
         {
             Debug.Log ("-Goal instance null-");
             yield return null;
         }
 
         //if returning user, load serialized goals data, else load goals excel data
         if(returningUser)
         {
             Debug.Log ("-In SetGoalData before LoadData()-");
             GoalDatabase._instance.LoadData();
         }
         else
         {
             Debug.Log ("-In SetGoalData before ImportData()-");
             GoalDatabase._instance.ImportData();
         }
     }

//etc... }

Similarly, I set up the GoalDatabase Singleton like this:

 public class GoalDatabase : MonoBehaviour {
 
     public static GoalDatabase _instance;
     
     void Awake()
     {
         Debug.Log ("In GoalDatabase Awake()");
         if (_instance == null) 
         {
             Debug.Log ("Instance was null");
             /*Setup this instance*/
             DontDestroyOnLoad (this.gameObject);
             _instance = this;
             
         } 
         else if (_instance != this) 
         {
             Destroy(this.gameObject);
         }
         
 //        if (_instance == null)
 //        {
 //            Debug.Log ("At end of GoalDatabase Awake(). _instance is still null");
 //        } 
 //        else 
 //        {
 //            Debug.Log("==========AT END OF GOALDATABASE Awake(). _instance IS NOT NULL==========");
 //        }
     }


It seems like the coroutine should wait until the GoalDatabase instance is set, then everything should be in line. BUT the GoalDatabase reference, as the coroutine sees it, is ALWAYS NULL. I am especially baffled, because the debug statements in the GoalDatabase (currently shown as commented out) indicate that it is in fact not null. What am I missing? Thanks for the help.

Edit: both these scripts are attached to a game object in the scene (separate GOs).

Comment
Add comment
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

1 Reply

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

Answer by Xtro · Jun 04, 2014 at 08:07 PM

I copy pasted your code to a fresh new project. There is no problem. It's working as expected. I ofcourse had to disable some lines to be able to copile it. Here is my version of your code. Put it in a fresh new project and test.

 using UnityEngine;
 using System.Collections;
 
 public class User : MonoBehaviour
 {
 
     public static User userInstance;
 
     void Awake()
     {
         Debug.Log("In User Awake()");
 
         //setup instance if this is the only copy
         if (userInstance == null)
         {
             /*Setup this instance*/
             DontDestroyOnLoad(this.gameObject);
             userInstance = this;
 
             /*Load the saved user data*/
             bool ReturningUser = true;// this.LoadData();
 
             /*Load or import goal data*/
             StartCoroutine("SetGoalData", ReturningUser);
 
         }
         else if (userInstance != this)
         {
             Destroy(this.gameObject);
         }
     }
 
 
     public IEnumerator SetGoalData(bool returningUser)
     {
         Debug.Log("In SetGoalData coRoutine and ReturningUser:" + returningUser.ToString());
 
         //wait for Goal Database object to instantiate
         while (GoalDatabase._instance == null)
         {
             Debug.Log("-Goal instance null-");
             yield return null;
         }
 
         //if returning user, load serialized goals data, else load goals excel data
         if (returningUser)
         {
             Debug.Log("-In SetGoalData before LoadData()-");
             //GoalDatabase._instance.LoadData();
         }
         else
         {
             Debug.Log("-In SetGoalData before ImportData()-");
             //GoalDatabase._instance.ImportData();
         }
     }
 }
 
 using UnityEngine;
 using System.Collections;
 
 public class GoalDatabase : MonoBehaviour
 {
 
     public static GoalDatabase _instance;
 
     void Awake()
     {
         Debug.Log("In GoalDatabase Awake()");
         if (_instance == null)
         {
             Debug.Log("Instance was null");
             /*Setup this instance*/
             DontDestroyOnLoad(this.gameObject);
             _instance = this;
 
         }
         else if (_instance != this)
         {
             Destroy(this.gameObject);
         }
 
               if (_instance == null)
               {
                   Debug.Log ("At end of GoalDatabase Awake(). _instance is still null");
               } 
               else 
               {
                   Debug.Log("==========AT END OF GOALDATABASE Awake(). _instance IS NOT NULL==========");
               }
     }
 }
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 r005t · Jun 04, 2014 at 08:57 PM 0
Share

I had another another copy of the script inside the Editor folder (copied and pasted ins$$anonymous$$d of cut and paste on accident) and I think that was causing the issue (confusion of static instances?). When I removed the extra script it worked.

Thanks for the help though.

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.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Avoid NullReferenceException with Singleton 3 Answers

Instantiating a Coroutine? 2 Answers

Would setting this static cause a memory leak? 1 Answer

Playmode script recompilation - static vars lost? 0 Answers

Static reference cost 1 Answer


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