Leveling UP/ Experience system using PlayerPrefs

Heya guys, I almost have an idea on getting an experience system down using PlayerPrefs. Only trying to figure out how I would apply it to multiple characters not just the one.

I have it set as each time I kill an enemy I gain 10 exp, but unsure on how to make it not affect a separate character that starts on level 1.

        int experience = PlayerPrefs.GetInt("Experience", exp);
        void Update()
    {
        if(enemyHeath =< 1)
        {   
        PlayerPrefs.SetInt("Experience",PlayerPrefs.GetInt("Experience")+10);
        }
    
    if (PlayerPrefs.GetInt("Experience") > 500)
    
       {
          PlayerPrefs.SetInt("SavedLevelUp1", 500);
       }

Any idea please help and thank you in advance.

Hi, while it can be used for arbitrary storage, it isn’t really intended for savegame features as it is limited and very simple.

From docs:

Stores and accesses player preferences between game sessions.

Anyhow, if you still want to go on with it, I presume your issue is that you don’t know how to store experience for player 1 and for player 2 and so on…

You could have

// How many players exist here?
int numPlayers = PlayerPrefs.GetInt("NumberOfPlayer", 0);
// Select which player should play (or possibly create a new one)
int player = SelectAPlayerToPlayWith(numPlayers);

// Get the player specific pref keys
string keyExperience = string.Format("Player{0}.Experience", player);
string keySavedLevelUp1 = string.Format("Player{0}.SavedLevelUp1", player);

// ... your existing code with some modifications
int experience = PlayerPrefs.GetInt(playerExperience, exp);
void Update()
{
    if(enemyHeath =< 1)
        PlayerPrefs.SetInt(keyExperience, PlayerPrefs.GetInt(keyExperience) + 10);
    if (PlayerPrefs.GetInt(keyExperience) > 500)
        PlayerPrefs.SetInt(keySavedLevelUp1, 500);

Use a variable to Have your level, “PlayerLevel”, and use a math formula to have the experience required per level, 1st level 10exp, 2nd level 25exp, 3rd lvl 45 exp, etc…, so now your experience validation would be

if(experience > ExperienceOnLevel(Playerprefs.GetInt(PlayerLevel)))
{
//add a level to playerlevel, and add stats according to it
}

good luck and happy coding