- Home /
[SOLVED] Help with PlayerPrefs.SetInt
I have a script which regenerates one life every 30 minute and then i can subtract one life by clicking a button that activates a "Subtractlife" function. The subtract function bit looks like this
static var maxLives = 5;
static var lives = 5;
static var RegenInterval = 1800; // 30min * 60
static var nextLife = 0;
static function SubtractLife(){
if(lives <= 0)
return false;
if (lives == maxLives)
{
nextLife = UnixTimestamp() + RegenInterval;
}
PlayerPrefs.SetInt("nextLife", nextLife);
lives--;
PlayerPrefs.SetInt("lives", lives);
return true;
}
Now my problem is, that when i play my game in Unity, it works but when i play it on my phone it doesnt function properly. I think it is because i get this error "TrySetInt 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." But when i do as the error says and put the PlayerPrefs.SetInt to the start function, the error stops showing but the script doesnt function properly in the game. Am i doing something wrong?
I think your problem might be that you're using the PlayerPrefs within a static function?
Thanks for your respond, i have tried doing what you say, but it still gives me the error.
Your answer
Follow this Question
Related Questions
Assets/Editor/UpdateTreeColors.js(13,17): BCE0031: Language feature not implemented: UnityEditor. 1 Answer
Trying to send message with 2 variables, but getting error. 1 Answer
Trouble making money system 1 Answer
Incremental game need help 1 Answer
Access function (and variable) outside class in the class in Javascript. 1 Answer