• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Vitorcampea · Mar 05, 2014 at 03:27 AM · sceneplayerprefsjavainstance

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:

  1. MainMenu (Should display a button to start and a best score)

  2. Gameplay (player can play and do so score, at the end gameplay save it on PlayerPrefs

  3. 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);
 }
 
 
 
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Mar 05, 2014 at 03:28 AM 0
Share

It would appear that we need to see the whole of that GameMaster script - i think you have a } missing or something.

avatar image Vitorcampea · Mar 05, 2014 at 03:34 AM 0
Share

added :), i cut the useless partes

avatar image whydoidoit · Mar 05, 2014 at 03:45 AM 1
Share

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 GameMaster.

avatar image Vitorcampea · Mar 05, 2014 at 06:11 PM 0
Share

what do you suggest?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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...

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vitorcampea · Mar 05, 2014 at 06:17 PM 0
Share

y, they are switched on my post, thanks. i will correct

avatar image Vitorcampea · Mar 05, 2014 at 06:19 PM 0
Share

hum...thats a good question..why do i have static there...didnt even noticed

avatar image Vitorcampea · Mar 05, 2014 at 06:22 PM 0
Share

OW, that static! this happens when u stay programming until late time! that static was making no sense indeed! thanks :)

avatar image Dblfstr · Mar 05, 2014 at 06:35 PM 0
Share

Don't forget to save.

 function SaveProgress () {
     PlayerPrefs.SetInt("Player Score", Score);
     PlayerPrefs.Save();
     }
avatar image Vitorcampea · Mar 05, 2014 at 07:49 PM 0
Share

"By default Unity writes preferences to disk on Application Quit." i did forgot, thanks :o

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Question on playerprefs 2 Answers

Problems with pulling a slider value - (Making sensitivity Sliders) 0 Answers

Something is wrong with my unity 1 Answer

Use A button to set a color using playerprefs in a different scene? 2 Answers

How do I stop a singleton from trying to load itself again when I change scenes? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges