- Home /
Static singleton is (not nullref -empty-) but assigned to "null" in the other Scenes
as Mentioned in title a weird effect of going to next scene by static variable singleton I always use singleton and its always working correctly but not this time .
I'm not using dontDestroyOnLoad of course cuz its a static var
I can work around the issue by using dontDestroy but am here to learn sth new about singeltons any body could help? Thanks
TheCode
public static DashBoard_Ref DbR_Instance;
void Start()
{DbR_Instance = this; }
forget to say its working in calling its methods (no nullref excep) but the problem showed up to me when I intended to use {If check} --> if(DashBoard_Ref.DbR_Instance != null) its false means its null but with no nullexception -when using methods without if- as mentioned ..
forget to say its working in calling its methods (no nullref excep) but the problem showed up to me when I intended to use {If check} --> if(DashBoard_Ref.DbR_Instance != null) its false means its null but with no nullexception when using methods without if as mentioned ..
Answer by Spip5 · Feb 24 at 12:09 AM
Have you tried making it a static class instead ? I don't see the full script, but I suppose it is running in a Monobehaviour.
I'm not using dontDestroyOnLoad of course cuz its a static var
The both are not related. For a Singleton instance of a monobehavior class, the instance acts as a "link" to a component, which is attached to a gameObject.
Should this object be deleted (changing a scene for example), then your singleton instance returns null. If you do not need the monobehavior components then do a static class
public static class DbR
{
// your functions and variables
}
and access it with "Dbr.myVariable" or "Dbr.myFunction()" . This way, it will act as a singleton, and won't be impacted by scenes changes, because it won't be attached to a GameObject (because it is not a monobehavior). If you need it to be a monobehavior, then either use DontDestroyOnLoad or load your next scene additilvely (more complexe).
ok nice so instance singleton is just an address for the Monobehaviour which is not still running while in another scene , I just don't know how it was working before with same weathers between scenes and don't know how without using the if statement its all running right
DashBoard_Ref.DbR_Instance.DoSth(); // that's working correctly and also not giving null excep
Thanks
hard to say without the code. Can you share your DashBoard_Ref script ? Also, can you share the line where the new scene is loaded ? EDIT : in your new scene, you do not have a gameobject with this script, right ?
Answer by haruna9x · Feb 24 at 04:11 AM
This problem is caused by the order in which the scripts are executed. If you call DashBoard_Ref.DbR_Instance before DashBoard_Ref's Start() function is called, it will be null. To fix this problem, you can assign static variable in Awake() function, and use it only in Start() function. You can also edit the order in which the scripts are executed (not recommended). Order of execution for event functions
thanks , but am using the instance in another scene after that Dashboard already being created before . i did also tried using Awake() its the same
Are you sure that there is at least one instance of the DashBoard_Ref component that is active on the Hierarchy window after you have switched the scene. Because you are not using DontDesstroyOnLoad on the gameObject, by default it will be destroyed, and DbR_Instance will now become null.
true it must refers to a null , but what I was confused by is why its working in calling function & why also there is no null exception -: shBoard_Ref.DbR_Instance.DoSth(); // that's working correctly and also not giving null excep
so I was assuming (bcuz this working) that instance(singleton) is not working like an address refers to a comp, but an clone object that caches everything in the component
I think I know now , will try to explain it later i
If you want to talk about the check null operator, this blog can help.
Your answer
Follow this Question
Related Questions
Static reference cost 1 Answer
Static Array - Throwing NullReference 1 Answer
Unity : Singleton ScriptableObjects resets after play 1 Answer
Static List< GameObject> in Singleton 1 Answer
In which cases I should use static? 1 Answer