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 ImKyleConway · Aug 15, 2020 at 08:29 PM · statichighscorelifetime

Add points to a lifetime score, once.

Hi, I am making a game with multiple levels, and at the the end of each level, you have a total amount of money (score), I would like to display the lifetime total of of that money on the main menu. However when I enter a new level it is displayed as $0 for that level, and then that score at the end added to the total as well.

Here are the two codes I am using:

Score:

 using System.Collections;
 using System.Collections.Generic;
 using System.Globalization;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Score : MonoBehaviour
 {
 
     public static int levelMoney = 0;
     public static int totalLevelMoney = 0;
     public TotalScore totalScore;
     public GameManager gameManager;
 
     Text money;
 
     // Start is called before the first frame update
     void Start()
     {
         //Find Money UI
         if (GameObject.Find("Money") != null)
         {
             money = GameObject.Find("Money").GetComponent<Text>();
         }
 
         //Find TotalMoney UI
         if (GameObject.Find("TotalMoney") != null)
         {
             totalScore = GameObject.Find("TotalMoney").GetComponent<TotalScore>();
         }
 
         //Find GameManager
         gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
 
         //Reset Score to 0 at beginning of level
         levelMoney = 0;
 
     }
 
     // Update is called once per frame
     void Update()
     {
         money.text = "$" + levelMoney;
 
         if (gameManager.GameOver == true)
         {
             totalLevelMoney = totalLevelMoney + levelMoney;
         }
     }
 }

Total Score:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class TotalScore : MonoBehaviour
 {
     public static int totalScore = 0; 
     Text totalMoney;
 
     // Start is called before the first frame update
     void Start()
     {
         //totalScore = totalScore + 1;
         totalMoney = GameObject.Find("TotalMoney").GetComponent<Text>();
     }
 
     // Update is called once per frame
     void Update()
     {
         totalMoney.text = "$" + (totalScore + Score.levelMoney);
     }
 
     public void PayOut()
     {
         
     }
 }
 

Everything is working fine, after being a level and getting say $7, it displays $7 on the main menu, however going back into a level and getting say $3, it now says $3 on the menu. I know this is due to the line levelMoney = 0;in the first script.

How can I add the level money to the total money once, and have the total remember that to be able to add more later?

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
1
Best Answer

Answer by highpockets · Aug 15, 2020 at 08:50 PM

You are going to have to incorporate data persistence of some sort. The way you choose is up to you.


An easy built in option if you’re not worried about people changing the value manually on their devices, you can use the PlayerPrefs and just store that “int” in there and nowhere else. It will persist throughout each level.


A more recommendable and secure option is to use a binary formatter and save things like this deeper in the file system (at a persistent data path). This way the data is saved in binary and is not so easily accessible in the event that someone wanted to cheat.


If this is something that is just going to need to persist during each time the game is open and not necessarily between sessions, then you can use the do not destroy on load function to make the game object persist between scenes.

Cheers

Comment
Add comment · Show 1 · 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 ImKyleConway · Aug 16, 2020 at 09:48 PM 0
Share

Great answer thank you. I was hoping to try to pull it off with some static variables, that is one thing I liked more about Game $$anonymous$$aker, them having global variables that can be accessed in any script, and any scene. But I ended up using the PlayerPref method you suggested, doing something like this:

void PayOut() { if (game$$anonymous$$anager.GameOver == true) { lifetimeScore = (PlayerPrefs.GetInt("TotalScore") + level$$anonymous$$oney); PlayerPrefs.SetInt("TotalScore", lifetimeScore); Debug.Log("Payout!"); } }

I dont want anyone messing with this value, so i will definitely look into encrypting or going to a different method in the future, 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.

Follow this Question

Answers Answers and Comments

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

Referencing static var 1 Answer

How to access script from another script in an object? 1 Answer

Function can't be called 0 Answers

Lighting: Should I split my big mesh-objects into small ones? 0 Answers

Strange shadowing when I bake 0 Answers


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