how to stop enum to reset

I am trying to find a way from which I can stop a enum to reset when player quits the game/changes scene and any other possibilities because My game’s shop works on the enum state and I want the enum to don’t reset .How can I do that? If any other information is needed let me know, Thanks.

Anything you do in Unity, every variable, every script, every object, is reset to its default empty value. However, the way you get around this is by saving the state, and loading it again. States for complicated objects have to be interpreted, since you can only save simple data types, such as strings, bools and numbers etc.


One way you can save and load an enum, is convert it to a string, save that using PlayerPrefs, then every time you start your game, you load the string from PlayerPrefs, and interpret that string to set the enum to its saved value.

Here’s a script:

public class SaveAndLoadEnum : MonoBehaviour
{
    const string key = "EnumValue";
    public States state;

    public enum States
    {
        State1,
        State2,
        State3
    }

    private void Start()
    {
        LoadEnum();
    }

    void LoadEnum()
    {
        string loadString = PlayerPrefs.GetString(key);

        System.Enum.TryParse(loadString, out States loadState);
        state = loadState;
    }

    public void SaveEnum()
    {
        string saveString = state.ToString();

        PlayerPrefs.SetString(key, saveString);
        PlayerPrefs.Save();
    }
}

@Ashmit2020