static class disappear when reloading the same scene?
I have a static class:
public static class SharedBetweenScenes {
public static string value;
}
In a script within a GameObject of the Scene A, I assign a value as follows: SharedBetweenScenes.value = "hi duby duby";
I load the Scene B. I can get the stored value doing: string value = SharedBetweenScenes.value;
Unfortunately, if a load the Scene A again and try to assign a different value: SharedBetweenScenes.value = "hello dude!";
the debugger says The name 'SharedBetweenScenes.value' does not exist in the current context.
Furthermore, if I load the Scene B, SharedBetweenScenes.value will have the old value of "hi duby duby"
instead of the new value "hello dude!"
What is going on here? I also tried the singleton code proposed here with the exactly same problem.
Answer by TBruce · Apr 15, 2016 at 05:53 PM
@chelder Because SharedBetweenScenes is not a MonoBehaviour class which means you can not attach it to a GameObject means you are leaving something out in your question leaving for some guess work.
If SharedBetweenScenes was created as a MonoBehaviour class then you could do something like in the Awake() function
DontDestroyOnLoad (transform.gameObject);
Since this does not seem to be the case you can look to do a few different things (you can try one or a combination of)
You could try placing DontDestroyOnLoad within the class the uses that SharedBetweenScenes class.
You could save and load your data using PlayerPrefs.
Add [System.Serializable] above all class defines.
The third option did not work:
[System.Serializable]
public static class SharedBetweenScenes
I also tried adding:
[SerializeField]
public static string value;
I'm not sure what I'm doing when I wrote the above code, by the way.
The second option does not seem for me as the description says: "data is stored (persisted) on the device". I don't want to keep the data between executions of the game.
I can't do the first option as I don't have any Start function or constructor on it. The SharedBetweenScenes static class only stores static values.
A $$anonymous$$onoBehaviour class cannot be a static class. I have used a static class in order to access it anywhere without having to attach the script to a GameObject. It must be something I don't know about Unity, so it does not work.
@chelder I find your code working fine
public static class SharedBetweenScenes
{
public static string value;
}
To better help you I would need to know more. But after looking at your original question I see the code
string value = SharedBetweenScenes.value;
"string value" is a temporary value and the only thing that will be static is SharedBetweenScenes.value. You need to access SharedBetweenScenes.value directly when reading and writing.
O$$anonymous$$G it was an if clause in a wrong place. Nothing related with the static class. So much time trying to figure out was going on, obsessed about the static stuff and the singleton stuff. Somehow, you allow me to unblock my $$anonymous$$d to find the silly problem. Thank you very much! I'm going to choose your answer as the correct one because I think it is a very good generic answer for anyone who search for a similar question. Thank you again! :)
Your answer
Follow this Question
Related Questions
Singleton Null Static Reference 1 Answer
Non-Static Member 1 Answer
global static function returns nullReference error [unityscript] 2 Answers
Setting objects that are large-scaled to static causing Unity to be stuck in '5/11 Clustering' 0 Answers
Object Reference not set to an instance of object / printing a variable from another scene 1 Answer