- Home /
What for are PlayerPrefs with 2 or more Scenes?
Hello, i am new in unity and i am reading everything i can, tutorials,youtube,etc
Well, i am developing a game, at this time it has 3 scenes:
MainMenu (Should display a button to start and a best score)
Gameplay (player can play and do so score, at the end gameplay save it on PlayerPrefs
RetryMenu (Should display best score and last score)
This variable Score is Stored on Scene Gameplay and on a script called "GameMaster", this script controls the game and has some important variables like Score. At the end of game i run this code
PlayerPrefs.SetInt("Player Score", Score);
When the game ends i call the Retry Scene and i try to read from playerPref
LastScore.text = PlayerPrefs.GetInt("Player Score").ToString();
It returns a error
Assets/Scripts/GameMaster.js(104,40): BCE0020: An instance of type 'GameMaster' is required to access non static member 'Score'.
My question is, why do i need playerpref to save information if i cant use them outside Scenes? Or can i? If yes, how can i pass the instance? hum...
(to resolve this problem i am thinking on writing on a txt and then read it each time i load a scene, is this a bad?)
EDIT: added GameMaster script
//Player
var Score: int = 0;
function Awake ()//copied this from some tutorials to not lose this instance, dunno if is needed
{
DontDestroyOnLoad (transform.gameObject);
}
function Update ()
{
...
if(Mathf.Round(Stamina) <= 0)
{
EndGame();
}
}
function EndGame()
{
SaveProgress();
Application.LoadLevel("Retry");
}
static function SaveProgress () {
PlayerPrefs.SetInt("Player Score", Score);
}
It would appear that we need to see the whole of that Game$$anonymous$$aster script - i think you have a } missing or something.
So there's your problem - you SaveProgress is a static but your Score is not. Either make Score static or make SaveProgress non-static or find a way of getting the current instance of Game$$anonymous$$aster.
Answer by perchik · Mar 05, 2014 at 06:14 PM
It appears that your problem is that you're not using PlayerPrefs correctly.
To save to player prefs, you use SetInt.
To retrieve the value, you use GetInt.
Which is backwards from what you described.
I also don't quite understand why you'd have a static save progress function...
y, they are switched on my post, thanks. i will correct
hum...thats a good question..why do i have static there...didnt even noticed
OW, that static! this happens when u stay program$$anonymous$$g until late time! that static was making no sense indeed! thanks :)
Don't forget to save.
function SaveProgress () {
PlayerPrefs.SetInt("Player Score", Score);
PlayerPrefs.Save();
}
"By default Unity writes preferences to disk on Application Quit." i did forgot, thanks :o
Your answer
Follow this Question
Related Questions
Preventing first instance of object from starting on screen 1 Answer
My MainCharacter is not being instantiated when awake? 1 Answer
How Can I save my values with Toggles in UI canvas 4.6 through scenes? 3 Answers
Question on playerprefs 2 Answers
How to save gameobject values with respect to scene? 1 Answer