- Home /
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.
Answer by Statement · Mar 06, 2013 at 02:47 PM
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);
Cheers, Statement. It's not so much as a player(s) but characters themselves, in the game I'm designing there is 9 unlock-able characters. So that being said would that apply for the "NumberOfPlayer"? sorry guys, I'm trying to get my head around it but it's still not clicking as fast as I want it too.
An Idea that came to my head was that, since each character has a different weapon I was thinking of something like:
if(other.gameObject.tag =="Sword")
{
PlayerPrefs.SetInt("$$anonymous$$nightExperience" + 10);
Debug.Log("Gain 10exp");
//
}
if(other.gameObject.tag =="Dagger")
{
PlayerPrefs.SetInt("RogueExperience" + 10);
Debug.Log("Gain 10exp");
//
}
Then apply this script to each character with a 'prefs' script with;
knightLevelReached = PlayerPrefs.GetInt("$$anonymous$$nightExperience");
public int knightLevelReached = 0;
void Update()
{
if(knightLevelReached > 100)
{
Debug.Log("Reached Level 2")
}
}
Any thoughts?
Answer by poncho · Mar 05, 2013 at 03:19 PM
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
Thank you for the reply. But I'm not sure if I understand this, but pretty much just replace the exp code with what you have given me? Also how would this work on separate characters? Sorry, I'm new to program$$anonymous$$g and have just been working on level unlocks which effects the whole game and the same with coins collected. ^^
no, use the logic of having Independent variable for your characters, PlayerLevel1, PlayerLevel2,... and also having ExpPlayer1, ExpPlayer2,... the ExperienceOnLevel $$anonymous$$ethod, should return your calculated exp per level that in case your experience is above that amount, then your player will get a levelup, for the new in program$$anonymous$$g, i would recommend, c# for dummies, GREAT book to learn from start. happy coding
Your answer
![](https://koobas.hobune.stream/wayback/20220613093846im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Differences between mobile save/load data 1 Answer