- Home /
PlayerPrefs
Ok I have been struggling for a fair amount of time now and I am not that great at coding as is =(. Basically I would like to save my currentScore variable so that when the level is finished I can then load it up in a new level. Below is the code and I would very much appreciate any help. Thank you for your time.
public var currentScore = 0;
var increaseScore = 20;
var myStyle : GUIStyle;
function playerScore()
{
currentScore += increaseScore;
}
function OnGUI ()
{
GUI.Label (Rect (Screen.width - 200,0,200,50),"Score: " + currentScore, myStyle);
}
Answer by Fehr · Jul 09, 2012 at 10:00 PM
If you where looking to use PlayerPrefs to save the currentScore Intager, you can do so like this:
PlayerPrefs.SetInt("CurrentScore", currentScore);
And retrieve it again in the next Scene, or the next run like this:
PlayerPrefs.GetInt("CurrentScore");
For more documentation/examples see the Unity Docs.
Answer by whydoidoit · Jun 11, 2012 at 10:20 PM
You could just make the variable static, then it will be one value for all instances - or you could have that object not destroy itself on load (by calling Object.DontDestroyOnLoad()).
public static var currentScore = 0;