Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
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 Game$$anonymous$$aster 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 Game$$anonymous$$aster.

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 program$$anonymous$$g 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.

Follow this Question

Answers Answers and Comments

23 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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges