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 ExcitedMormon · Sep 23, 2014 at 09:31 AM · c#playerprefs

How To Add PlayerPrefs Scores?

Hello again. This might be a simple question, but how can I add a saved score from one scene to another? So like let's say I have a score of 300, and I do something to load a different scene, played it, got 200 more points, headed back to the first scene and now the score is 500(300 + 200 = 500). How would I add those 200 points to the 300 score? Think of it as a bonus/special stage adding on to your regular score. Please I hope you can help me. My scoring script for scene1 that's attached to the Player:

 public class ScoringScript : MonoBehaviour {
     
     float playerScore = 0;
     public float guiPlacementY1;
     public float guiPlacementX1;
     Stage1 player;
     
     void Start()
     {
         player = GetComponent<Stage1>();
     }
     
     
     public void IncreaseScore(int amount)
     {
         playerScore += amount;
     }
     
     public void DecreaseScore(int amount)
     {
         playerScore -= amount;
     }
     
     
     void OnDisable()
     {
         PlayerPrefs.SetInt ("Score", (int)(playerScore * 5));
         PlayerPrefs.SetString("PlayerName", "High Score");
         PlayerPrefs.Save ();
         Debug.Log("Saved");
     }
     
     void OnGUI()
     {
         
         GUI.Label (new Rect(Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .5f, Screen.height * .4f), "Score: " + (int)(playerScore * 5));
     }
 }

And the script that displays the score:

 public class GameOver : MonoBehaviour {
 
     int score = 0;
     int highscore = 0;
 
     public float guiPlacementY1;
     public float guiPlacementY2;
 
     public float guiPlacementX1;
     public float guiPlacementX2;
     
 
 
     void Start () {
                 score = PlayerPrefs.GetInt ("Score");
                 highscore = PlayerPrefs.GetInt ("High Score");
                 Debug.Log ("M");
                 if (score > highscore) { //when player dies set highscore = to that score
                         highscore = score;
                         PlayerPrefs.SetInt ("High Score", highscore);
                         Debug.Log("ik");
                     
         }
     }
 
         
     void OnGUI()
     {
 
         GUI.Label (new Rect (Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .5f, Screen.height * .4f), "GAME OVER");
         
         GUI.Label (new Rect (Screen.width * guiPlacementX2, Screen.height * guiPlacementY2, Screen.width * .5f, Screen.height * .4f), "Score: " + score);
     }
 
 }
 
Comment
Add comment · Show 3
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 VIPINSIRWANI · Sep 23, 2014 at 09:56 AM 0
Share

Here you can use static data member and also you can access it in other script so you can easily update your score in multiple scene.

 public static int Score;

In Othes Script you can access it with class name

avatar image ExcitedMormon · Sep 23, 2014 at 05:55 PM 0
Share

So I make my score integer in my GameOver script public static? If so, after that how do I access it correctly?

avatar image KMKxJOEY1 · Sep 23, 2014 at 06:13 PM 0
Share

I posted an answer for your PlayerPrefs question, but if you want to use a static method, I would personally just have a GameObject to manage all persistence so it could call DontDestroyOnLoad(gameObject); and hold all of your stats.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KMKxJOEY1 · Sep 23, 2014 at 06:12 PM

 PlayerPrefs.SetInt("Score", PlayerPrefs.GetInt("Score",0) + score);

Comment
Add comment · Show 2 · 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 ExcitedMormon · Sep 24, 2014 at 02:33 AM 0
Share

Does that go in the scoring script? The scoring script is the script I use for the bonus stage for points. The script I use for Stage 1(the regular stage) is the same script but with a different name. I only use the Game over script for Stage 1.

avatar image ExcitedMormon · Sep 26, 2014 at 04:03 AM 0
Share

So does it or not? Sorry to bother but I need to know.

EDIT: I still haven't found out what to do. Does anyone have some tutorials I can watch/read?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Player lives script help 1 Answer

Weird GUILayout Scrollview Behavior 0 Answers

Changing Axis to Buttons 2 Answers

Need help calling a script to another keep getting errors 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