Hi. I have script that is some kind of score system. It’s working like I wanted. Now I need to save variable value after level has been unloaded and load that value after getting again into same level. But after I load level again I get value of 0. This is my script:
#pragma strict
public var buck : int;
public var ScoreBoard : GUIText;
function Start ()
{
}
function Update ()
{
if (Input.GetKeyDown("space"))
{
buck += 1;
ScoreBoard.text = ("") +buck;
}
}
Now I’m saving variable value with this code:
function setScores()
{
PlayerPrefs.SetInt("newScore", buck);
}
function getScores()
{
PlayerPrefs.GetInt("newScore");
}
And after level has been loaded again I have this code to load variable:
function OnLevelWasLoaded (level : int) {
if (level == 2)
{
ScoreBoard.text = ("") +newScore;
}
}
If I use +newScore I get error saying that there is no variable called newScore, but if I use buck variable I get value 0, as I guess it’s using value from the top where variable was declared. Any way I can fix this?
I can only assume that your setScores() and getScores() functions are being called from somewhere at appropriate times - your posted code doesn’t show that. However, the obvious problem is in getScores(). There, you’re not actually storing the value you’re retrieving from PlayerPrefs. A call to PlayerPrefs.GetInt() returns an int value, so you need to store that value for later use. Something like this:
function getScores()
{
buck = PlayerPrefs.GetInt("newScore");
}
Jeff
I did that but I still get same result. Here is my entire code:
#pragma strict
public var buck : int = 0;
public var ScoreBoard : GUIText; // GUI text for score
function Start ()
{
}
function Update ()
{
if (Input.GetKeyDown("space"))
{
buck += 1;
ScoreBoard.text = ("") +buck;
}
}
function setScores()
{
PlayerPrefs.SetInt("newScore", buck);
}
function getScores()
{
PlayerPrefs.GetInt("newScore");
}
function getScoresVar()
{
buck = PlayerPrefs.GetInt("newScore");
}
function OnLevelWasLoaded (level : int)
{
if (level == 2)
{
ScoreBoard.text = ("") +buck;
}
}
If I wasn’t clear enought, I’m closing this level with Application.LoadLevel("Some_level")
and I’m again loading this level with score script inside newly loaded “Some_level”. But it appears that I’m not saving variable value correctly.
Again, I’m left wondering if/when/where you call setScores(). Does that ever get called to save the score?
Should I delete function getScoresVar and put this: buck = PlayerPrefs.GetInt(“newScore”); inside getScores function? Sorry, I’m not so good at programming.
It doesn’t really matter what the function is called. Whatever it’s called though, you need the “buck = …” code to store the retrieved value…
Jeff
I added this:
function OnLevelWasLoaded (level : int)
{
if (level == 2)
{
buck = PlayerPrefs.GetInt("newScore");
ScoreBoard.text = ("") +buck;
}
}
and now after loading level again I always get 5, no matter how many times I press “space” to make buck variable higher. Any fix for this?
But, again, where is “setScores()” called from?
Thank you very much, I solved it. I called setScores() inside update function and now it’s working perfectly. Thank you once again!