Question on playerprefs

i have been working with different functions and values in coding, as i’m still learning. And im considering the prospects of using the specific functions with playerprefs like setint and getint. I understand how you. can set the value of a key, and retrieve it’s value, this is what i understand, but what I’m confused about is how the value is created in the first place.

If i were to set an int value on startup, and require the int value to be a certain number to unlock a character for a game for example, and have it initially set to 0. Upon completing an arbitrary condition, the int value is set to 1, and the character will be unlocked. But if I quit the game, Would it be reset on startup because of the setint function?

The jist of my question is how to create the int value once with a value of a number in the first time the game is run and not have it reset upon loading the game up again.

Use GetInt first. If the int does not exist it will return 0, or some other default value of your choice. This allows you to check if anything is in PlayerPrefs before you use SetInt

Edit: Just scanning the documentation now, you could also use HasKey

PlayerPrefs holds the data in a simple file so it won’t be deleted when you quit your game. However you need to load the value (using GetInt) when you start your game and save it (using SetInt) when you quit your application.

I see you’re worried about setting int to 0 everytime you start your game. You can just use PlayerPrefs.HasKey function in order to determine whether you alread stored a variable with a particular key or not. If there’s no data paired with your key, you can set your variable to 0.

void Start()
{
    if(PlayerPrefs.HasKey("charactersPoint") == false)
    {
        // There's no data named "charactersPoint" yet.
        // It seems like it's the first run of your game, 
        // feel free to create one
        PlayerPrefs.SetInt("charactersPoint", 0);
    }
}

Edit : You don’t want to store a sensitive data (such as yours in this case) with PlayerPrefs, anyone can edit it. As the name implies, it’s the player preferences. You’ll want to serialize your data for important data. Mike did an awesome tutorial about this subject. Good luck.