- Home /
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.
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! :-)
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?
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?
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
}
}
that makes sense, I won't be able to check for a while but when i do ill update this.
Your answer
Follow this Question
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