will switching scenes overwrite data?

So lets say we have a simple script and it adds +1 to an integer value everytime a key is pressed, then we put that data into a save file. Then we switch the scene to another scene, will the integer still have the value or the scene overwrites it?

Unless your object is persistent, it will. There are a few work arounds, such as saving to player prefs and loading on the next scene, or making the values and the scripts static. Another way would be to put your game manager in a separate scene, load it in async, and have that scene persist throughout your game,

Advancing on one of Mashimaro7’s suggestions:


Static classes don’t need to be attached to a gameObject, which means they cannot be destroyed when switching scenes, but will be lost when you stop playing. For example:

public static class DontDestroyTheseValues
{
    public static int Number;
}

No matter what scene you are in, if you write DontDestroyTheseValues.Number += ... it won’t switch back to its default number. Unfortunately, you cannot add MonoBehaviour to a static script. @greenklover