- Home /
LoadScene() and variables
Hello, do the values within my variables get reset each time a scene is loaded, or do they retain the values they had from the previous scene?
Answer by Ben 14 · Mar 01, 2011 at 04:36 PM
You may require a specific script/gameobject to persist between scene changes by using DontDestroyOnLoad
Answer by IJM · Oct 16, 2010 at 07:24 PM
All the scripts (Monobehaviours) attached to objects will be destroyed once you leave the scene. If you wan't to retain some values you can do something like this:
static class PermanentValues
{
public static float PlayersHealth;
}
This can be accessed from any other class like this:
...
PermanentValues.PlayersHealth = 15;
...
thank you, but how am I suppose to retain the values of health, score, money, current_weapon etc. when changing from one scene to the next?
I edited my answer, look at it again. I've given you an example of how to retain values.
Answer by Orion_78 · Feb 10, 2014 at 06:34 AM
To save values you could also use PlayerPrefs.SetFloat("Score", score);
Your answer
![](https://koobas.hobune.stream/wayback/20220613075901im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
allowSceneActivation give me error message 0 Answers
heavy scene loading 1 Answer
My button won't load a scene 1 Answer
Load another Scene after collecting 6 coins 1 Answer
Save & Load Game question 3 Answers