Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

Help with PlayerPrefs.SetInt

$
0
0
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?

Viewing all articles
Browse latest Browse all 1333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>