- Home /
Question by
Alter · Mar 11, 2013 at 03:50 AM ·
javascriptplayerprefsexperienceleveling
PlayerPrefs reset error
Not sure how else I can explain this but I have a working exp/leveling system however when I load the scene again it randomly jump to the next level and starts the exp at 0 (which happens if you gain a level). Yet sometimes it works perfectly, any ideas guys?
public var expTillNextLevel : float;
var expTexture : Texture;
var expGainedTexture : Texture;
var expGainedMaxTexture : Texture;
var expBackTexture : Texture;
public var expTotal : int;
public var killsTotal : int;
public var Level : int = 1;
public var Exp : int = 1;
function Awake()
{
expTillNextLevel = 100;
//PlayerPrefs.SetInt("Exp",0);
//PlayerPrefs.SetInt("Level",0);
//PlayerPrefs.SetInt("Level",1);
if (PlayerPrefs.GetInt("Exp") == 4500)
{
print ("Level 10");
PlayerPrefs.SetInt("Exp", PlayerPrefs.GetInt("Exp")+0);
LevelUp();
}
}
function Update ()
{
if (Input.GetKeyDown("r")) //Resets everything
{
PlayerPrefs.SetInt("Exp",0);
PlayerPrefs.SetInt("Level",0);
expTillNextLevel = 100;
killsTotal = 0;
expTotal = 0;
}
if (PlayerPrefs.GetInt("Exp") >= expTillNextLevel)
{
print ("Level Up");
LevelUp();
}
if (PlayerPrefs.GetInt("Level") < 2)
{
PlayerPrefs.SetInt("Level",1);
}
if (PlayerPrefs.GetInt("Level") < 10)
{
PlayerPrefs.SetInt("Exp", PlayerPrefs.GetInt("Exp")+1);
killsTotal += 1;
expTotal += 1;
}
if (PlayerPrefs.GetInt("Level") > 10)
{
PlayerPrefs.SetInt("Exp", PlayerPrefs.GetInt("Exp")+0);
}
}
function OnGUI()
{
GUI.Label (new Rect (10, 250, 1000, 20), "Level:" + PlayerPrefs.GetInt("Level"));
GUI.Label (new Rect (10, 300, 1000, 20), "Exp:" + PlayerPrefs.GetInt("Exp"));
GUI.Box(Rect(Screen.width/2 - 250, Screen.height - 95, 500, 30), "Current Level: " + PlayerPrefs.GetInt("Level"));
GUI.DrawTexture(Rect(Screen.width/2 - 250, Screen.height - 35, 500, 10), expBackTexture);
GUI.DrawTexture(Rect(Screen.width/2 - 250, Screen.height - 35, (PlayerPrefs.GetInt("Exp")*500)/expTillNextLevel, 10), expGainedTexture);
GUI.DrawTexture(Rect(Screen.width/2 - 250, Screen.height - 35, 500, 10), expTexture);
GUI.Label(Rect(Screen.width/2 - 40, Screen.height - 130, 500, 30), "Total Kills: " + killsTotal + "");
GUI.Label(Rect(Screen.width/2 - 45, Screen.height - 115, 500, 30), "Total EXP: " + expTotal + "");
}
function LevelUp()
{
PlayerPrefs.SetInt("Level", PlayerPrefs.GetInt("Level")+1);
PlayerPrefs.SetInt("Exp",0);
expTillNextLevel += 100;
}
Any help will be greatly appreciated and thanks in advance.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613094202im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Need help with a proficiency system(experience system) 1 Answer
PlayerPrefs Help 1 Answer
new user very easy c# question regarding syntax 1 Answer
How would I transfer these into PlayerPrefs? 0 Answers
Is there a way to clear all the PlayerPrefs with names that start with a specific string? 1 Answer