- Home /
starting values happening only once in Start()
so i have a script, and it runs a whole series of different things. in the game, you can set your starting values through the maze to whatever you want. Like bombs, ammo, etc. But when you set them, and play, eventually you will want to get to the goal. When you touch the goal, it will build another randomly generated maze. But, the values wont save through that process. It will revert. Lets say you use 20 ammo out of 100. You will have 80 ammo left, but when you touch the goal, it will go back to 100 ammo. I am very confused. The values wont save through loading the scene over again.
Answer by pophead2 · Mar 24, 2018 at 08:03 PM
They won't save when reloading a scene. check these out: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
Answer by Priyanka-Rajwanshi · Mar 25, 2018 at 08:21 AM
The values of variables do not save when a scene is reloaded as the script runs again and the variables are reinitialised to their default values.
You can make these variables as static in order to make the values stay within the game even when you reload the scene.
OR
If you want these values to save and use them the next time you open the game and start playing, use PlayerPrefs. https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
I have checked, and all of the variables are static. I havent tried PlayerPrefs yet but thank you.