• 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 CoreyCoder · Sep 16, 2013 at 07:27 PM · score

saving the score on level change

i m making a score system for a game, and i want to save the score when moving over to the next scene, and then taking that score that i ve saved, and increment it every second. this is what i have:

 void Start () 
 {
     score = 1;
     MaxLevelScore = 600;
     
 }
 
 // Update is called once per frame
 void Update () 
 {        
     
     
     score++;    
 
     saveScores();
     changeLevel();
 }
 


 public void changeLevel()
 {
     if (score == 600)
         {
         //save the score to carry over to the next level
         
             
         // load the level index here
             Application.LoadLevel("Level2");
             saveScores();
         }    
     if(Application.loadedLevelName == "Level2")
     {
         getSavedScores();    
     }
 
 }

 //possible if the save functionality intreacts with this
 public void saveScores()
 {
     if (score == 600)
     {
     // setting the score
         PlayerPrefs.SetInt("MaxLevelScore", 600);
         
     }
 }
 public int getSavedScores()
 {
     
     int tempScore = PlayerPrefs.GetInt("MaxLevelScore");  
     
     return tempScore;
 }
 
 
 public  int getScore()
 {    
     
     
     return score;
 }
Comment
Add comment · Show 5
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 ShadoX · Sep 16, 2013 at 07:50 PM 0
Share

Sorry, but what exactly is the problem? I'm assu$$anonymous$$g that the answer is http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html which you could use to keep the object between scenes assu$$anonymous$$g that this is/was the problem.

avatar image CoreyCoder · Sep 16, 2013 at 07:55 PM 0
Share

i m trying to take the score from level 1, keep that score and continue incrementing by 1 on level change, i have tried using the dontdestroyonload, it keeps the value but it dosent keep incrementing

avatar image ShadoX · Sep 16, 2013 at 08:31 PM 0
Share

I do not see anywhere where you would increase the score besides the "score++". The problem to increase the score after changing the level lies in this code:

 public void saveScores()
 {
     if (score == 600)
     {
     // setting the score
        PlayerPrefs.SetInt("$$anonymous$$axLevelScore", 600);
  
     }
 }

first of all you're only saving the score when "score" is 600 (constant value) and you'r also not saving the score, but ins$$anonymous$$d a constant. (600)

So ins$$anonymous$$d of that you should use..

 public void saveScores()
 {
        PlayerPrefs.SetInt("$$anonymous$$axLevelScore", score);
 }

Now you're saving the actual value of the "score" variable to the preFab which means that it won't get "reset" because you're not constantly setting it to 600.

avatar image oliver-jones · Sep 16, 2013 at 08:34 PM 0
Share

@ShadoX1 - that shouldn't make a difference because the save within saveScores will only be called with score == 600, so there is no difference between 600 and score, as at that point - they will both be the same.

avatar image ShadoX · Sep 16, 2013 at 09:06 PM 0
Share

Sorry I mean this additionally to your code

 if (score >= 600)

1 Reply

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

Answer by oliver-jones · Sep 16, 2013 at 07:56 PM

Could the problem be that you're loading your level, and then saving it (which wont get executed):

 // load the level index here
 Application.LoadLevel("Level2");
 saveScores();

Try swopping those two around. Always have your 'Application.LoadLevel' the last thing to execute.

--- Update ---

Also, try replacing all your:

 if (score == 600)

To (just incase your update misses it by one value):

 if (score >= 600)
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 CoreyCoder · Sep 16, 2013 at 08:12 PM 0
Share

thanks for the suggestion, i did that, i also looked at my value, it is incrementing, its just not showing on my display, so i think i just need to find out a way, to make the display show my incremented value currently all its displaying is "600" not increasing, but my level is resetting

avatar image oliver-jones · Sep 16, 2013 at 08:19 PM 0
Share

Oh right - by display, do you mean a GUI? Are you passing 'score' to a GUI label?

avatar image CoreyCoder · Sep 16, 2013 at 08:33 PM 0
Share

yea, well its more of a GUI text, its in a different script, i prob should have uploaded that, sorry here's my GUI Script: using UnityEngine; using System.Collections;

 public class GUI$$anonymous$$enu : $$anonymous$$onoBehaviour {
     public bool paused = false;
     public Score scoreTest;
     public int scores;
     public GUISkin Necro;
     public GUIText totalScoreText;
     private int totalScore;
     void Awake()
     {
 
         
     }
     void start()    
     {
         DontDestroyOnLoad(scoreTest);
         scoreTest.GetComponent<Score>();
     }
     void update()
     {
 
         scoreTest.score++;
         OnGUI();
 
     }
 
     void OnGUI () 
     {
             
         
         totalScoreText.text = "Score: " + scoreTest.getScore();
         Pause();
     
     }
     
     public void Pause()
     {
         
         GUI.skin = Necro;
 
         if(paused == false)
         {
             if(GUI.Button(new Rect(20,100,100,50), "Pause"))
             {
                 Time.timeScale = 0;
                 paused = true; 
                 guiText.text = "paused";
             }
         }
         else if(paused == true)
         {
             GUI.Button(new Rect(100,100,100,50), "Options");
             if(GUI.Button(new Rect(20,100,100,50), "Resume")|| Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape)) 
             {
                 Time.timeScale = 1;
                 paused = false; 
             }
             
 
 
         }
         
     
     
         
         }     
     }
avatar image oliver-jones · Sep 16, 2013 at 08:41 PM 0
Share

Well - I see your problem, you need a Capital S on start, and a Capital U on Update, and remove OnGUI from your Update function as it gets called automatically anyway.

avatar image CoreyCoder · Sep 17, 2013 at 01:00 PM 0
Share

Sweet, thanks, i think in my confusion i overlooked the small stuff like caps on my functions. well on the next level the score is incrementing, and its carrying over from the last scene. thanks again

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

How do I fix this Null Reference error and update my score? 2 Answers

Score going up every second 1 Answer

distance based score system 3 Answers

Saving Score In Player Prefs? 3 Answers

Need Help with Dual Scoring System C# (score over time and kill score) 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