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 Theros_Daserath · Sep 30, 2020 at 02:51 PM · nullreferenceexceptionsingletonscene-change

Nullreferenceexception when singleton called by object loaded from previous scene that had another copy of that singleton

The structure of this is a bit complex so bear with me:


In Scene number 1 there is:

  • 1 dont destroy on load object that calls on a singleton through Name.Instance.Function() (gamemaster)

  • 1 prefab holding a singleton.


in Scene number 2:

  • The same dont destroy on load object that carried over from scene 1

  • a new copy of the singleton prefab that is distinct from the first one


This is giving out a null reference exception when SingletonName.Instance.Function() is called in Scene 2 but only on a Build/not in the editor.

I'm guessing it has something to do with the Instance call not being for the "same" script as when the object was created but I can't be sure.

The fix was to simply put the gamemaster script inside the prefab for this scenario but I'd like to understand what's going on and why since my code is meant to be used by several different projects/people in several different conditions.

Comment
Add comment · Show 2
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 _dns_ · Sep 30, 2020 at 04:10 PM 0
Share

Hi, it seems impossible to know what could happen without code, and it maybe too much code for this website, so my advice will be more general: learning how to use the debugger (visual studio or other) attached to the Editor or to a standalone build is extremely helpful in many situations like this one. It's a small cost for many many benefits! :-)

avatar image CrazyNito · Sep 30, 2020 at 09:52 PM 0
Share

This is why singletons are bad, do the research. Aside from that, you should check in singleton if instance of it already exist and delete it. Something went wrong with your singleton instantiation. Why is there a new copy on singleton in scene 2?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by l3mon · Sep 30, 2020 at 08:39 PM

I‘m not sure about the Name.x part as to which reference is stored in your object from scene 1. If you try to recatch the singleton prefab (find by tag or similar), does that work?

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
1

Answer by rh_galaxy · Sep 30, 2020 at 10:49 PM

If your singleton is implemented like the code below (most examples found are), my guess is that the 2nd time it runs in the 2nd scene, theGM will not be updated and will keep the now invalid reference. If you are going to init the singleton more than once, you need to set theGM = null when it is destroyed when unloading the scene since it's static. I think this explains it. When using Do not destroy on the singleton there won't be a problem since the object will never be destroyed after creation.

 public class GameMaster : MonoBehaviour
 {
     public static GameMaster theGM = null;
     void Awake()
     {
         if (theGM == null) {
             theGM = this;
         } else if (theGM != this) {
             //enforce singleton pattern, meaning there can only ever be one instance.
             Destroy(gameObject); //<- this makes OnDestroy() be called, be careful if that causes problems
             return;
         }
         //any other code will only run once
         //...
         //here goes DontDestroyOnLoad(gameObject) normally
     }
 }
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 Theros_Daserath · Oct 02, 2020 at 07:31 PM 0
Share

that makes sense, I won't be able to check for a while but when i do ill update this.

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

144 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 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 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 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 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 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 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

ToolBox singleton nullreferenceexception on reloading scene 1 Answer

Singleton good in one project but null references in other 1 Answer

Problem with Singleton and NullReferenceException? 0 Answers

Data from a simple singleton / static class return null 1 Answer

Null reference exception - singletone 0 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