Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TinyUtopia · Apr 21, 2010 at 03:34 PM · staticinstancesingleton

Avoid NullReferenceException with Singleton

I'm using a game singleton to store variables accross scenes, such as player score.

When I'm testing my various scenes I get a NullReferenceException for variables which are held in the singleton becuase the singleton is being made static in the first scene.

static var instance : GameManager; // identifies this script as a global item

Is there a way that I can make each scene load the singleton while testing, but prevent each scene from making an instance of it at runtime?

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

3 Replies

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

Answer by andeeee · Apr 21, 2010 at 04:10 PM

You can mark the object with DontDestroyOnLoad to prevent it from being destroyed during a scene change. However, you should check for the object's existence before doing this so that you don't end up with multiple copies. You could do this by trying to find the object before instantiating it:-

if (GameObject.Find("SingletonObject") == null) {
    var sng = Instantiate(Resources.Load("Singleton"));
    sng.name = "SingletonObject";
    DontDestroyOnLoad(sng);
}
Comment
Add comment · Show 3 · 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 TinyUtopia · Apr 22, 2010 at 09:49 AM 0
Share

I already have a DontDestroyOnLoad on the Singleton. So would simply checking for the Singleton object's existence and then if not found instantiating it work? e.g.

if (GameObject.Find("SingletonObject") == null) { var sng = Instantiate(Resources.Load("Singleton")); sng.name = "SingletonObject"; }

avatar image TinyUtopia · Apr 22, 2010 at 09:50 AM 0
Share

Ok, pasting code doesn't work in comments.

All I did was remove the line DontDestroyOnLoad(sng);

Because there is already a DontDestroyOnLoad on the Singleton script. Works for my needs, thanks

avatar image TinyUtopia · Apr 29, 2010 at 10:37 PM 0
Share

Oddly, $$anonymous$$y use of the singleton started causing Unity to crash on the scene that was parsing it. I changed all the variables that I was using in the singleton to static as Duck suggested.

Whenever on of the static vars changes it updates to playerprefs to be stored whenever a certain variable needed later in another scene.

avatar image
1

Answer by duck · Apr 21, 2010 at 04:11 PM

You're probably getting null reference exceptions because your singleton variables contain references to objects which were destroyed when the new level was loaded.

Rather than re-loading the singleton each time a scene is loaded, you should use DontDestroyOnLoad to preserve your object throughout all scenes, or - if you are only storing data in variables - simply just use static variables, and don't create a singleton instance of your class at all.

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

Answer by Cyclops · Apr 21, 2010 at 05:50 PM

I have a demo in the Forums that might be useful, Demo of a (singleton) LevelManager.

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

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

No one has followed this question yet.

Related Questions

Would setting this static cause a memory leak? 1 Answer

Singleton instance accessed in coroutine always null 1 Answer

An instance of type "x" is required to access non-static variable "y" 2 Answers

Is it necessary to null an instance on quit? 2 Answers

instance and static variables 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