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
0
Question by Sweex · Feb 28, 2014 at 05:32 PM · saveloadscore

Save variable value.

Hi. I have script that is some kind of score system. It's working like I wanted. Now I need to save variable value after level has been unloaded and load that value after getting again into same level. But after I load level again I get value of 0. This is my script:

 #pragma strict
 public var buck : int;
 
 public var ScoreBoard : GUIText;
 
 function Start () 
 {
     
 }
 
 function Update () 
 {
 
     if (Input.GetKeyDown("space"))
     {
         buck += 1;
         ScoreBoard.text = ("") +buck;
     }
         
 }

Now I'm saving variable value with this code:

 function setScores()
 {
     PlayerPrefs.SetInt("newScore", buck);
 } 
 
 function getScores()
 {
     PlayerPrefs.GetInt("newScore");
 }

And after level has been loaded again I have this code to load variable:

 function OnLevelWasLoaded (level : int) {
         if (level == 2)
         {    
             ScoreBoard.text = ("") +newScore;    
         }
 }

If I use +newScore I get error saying that there is no variable called newScore, but if I use buck variable I get value 0, as I guess it's using value from the top where variable was declared. Any way I can fix this?

Comment
Add comment
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

1 Reply

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

Answer by perchik · Feb 28, 2014 at 05:34 PM

If you actually call setScores(), then it should register a playerpref named "newscore" with the score value.

When you want to load the new scene and get it then its just

 var score: int = PlayerPrefs.GetInt("newScore");

which gets the value of playerprefs "newScore" and puts it into your new score variable.

Comment
Add comment · Show 3 · 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 Sweex · Feb 28, 2014 at 05:49 PM 0
Share

I did that and I get same result:

 public var buck : int = PlayerPrefs.GetInt("newScore");
 
 function OnLevelWasLoaded (level : int) {
         if (level == 2)
         {            
             ScoreBoard.text = ("") +buck;    
         }
 }
avatar image perchik · Feb 28, 2014 at 05:50 PM 0
Share

Can't do it outside of a function, you have to do that inside of a function call.

avatar image Sweex · Feb 28, 2014 at 05:59 PM 0
Share
 function OnLevelWasLoaded (level : int) {
         if (level == 2)
         {        
             buck = PlayerPrefs.GetInt("newScore");
             ScoreBoard.text = ("") +buck;    
         }
 }

Is this fine? Sorry, I'm not so good at program$$anonymous$$g. Also, I get this warning in console after loading this level again:

$$anonymous$$issingReferenceException: The variable ScoreBoard of 'Score' doesn't exist anymore. You probably need to reassign the ScoreBoard variable of the 'Score' script in the inspector. Score.OnLevelWasLoaded (Int32 level) (at Assets/Scripts/Level/Score.js:46)

Line 46: ScoreBoard.text = ("") +buck;

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

21 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

Related Questions

PlayerPrefs 2 Answers

Score and Level Loading 1 Answer

Score system Level Load 1 Answer

Playerprefs When the value I set is zero, there is a 2x increase in the part I get. when i close and reopen the game it only shows the last value. How can ı solve these? 1 Answer

Problems with saving/loading score with PlayerPrefs [C#] 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