I get this error: GetInt can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
#pragma strict
guiText.fontSize=Screen.width/25;
var highscore:int=PlayerPrefs.GetInt("highscore",0);
function Update(){
PlayerPrefs.SetInt("highscore",highscore);
guiText.text=highscore.ToString();
if(score>highscore){
highscore=score;
}
}
I don’t actually use “score” but I just wanted to show the code logic.
var highscore:int = PlayerPrefs.GetInt("highscore",0);
Call the function from within a function. The error message tells you what to do : instead move initialization code to the Awake or Start function.
var highscore:int = 0;
function Start() {
highscore = PlayerPrefs.GetInt("highscore",0);
}
I would consider a better method than setting the PlayerPrefs every update. Consider calling a function to assign score to PlayerPrefs once at the end of the players session (or if the app has been quit, paused, etc).