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 Ashmit2020 · Jan 12, 2021 at 06:21 AM · uiunity 2dscore systempointsshop

points system gets overrwriten

I am trying to make a point system that will display itself in (shop menu UI)Maybe this is not related to the question only telling where is the text located. So what my point system is intended to do is, save the score that the player has earned and in a playerpref display it in a TMPro text. But what the script is doing is overwriting the score for example: If the player earned 50 score in the first match and 30 score in the second match instead of adding 50+30 and displaying 80 it overwrites 30 so how can I fix that. These are the two scripts that make the procedure: This is the script responsible for the display:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro;

public class shop : MonoBehaviour { public TMPro.TextMeshProUGUI scoreText;

void Start () { scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString(); } }

This is the script responsible for saving score (I think problem related code is in die method)

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using System;

public class PlayerHealth : MonoBehaviour {

 public GameObject death;
 public int maxHealth = 100;
 public static int currentHealth;
 public HealthBar healthBar;
 // Start is called before the first frame update
 void Start()
 {
     currentHealth = maxHealth;
     healthBar.SetMaxHealth(maxHealth);
 }

 // Update is called once per frame
 void Update()
 {
  
 }
 
   

 public void TakeDamage(int damage) 
 {
     currentHealth  -= damage;
 
     healthBar.SetHealth(currentHealth);

     if( currentHealth  <= 0)
     {
         Die();
     }
 }

 void Die ()
 {
    Destroy(gameObject);
    audioman.PlaySound ("explosion");
    Instantiate(death, transform.position, Quaternion.identity);
    PlayerPrefs.SetFloat ("Highscore", ScoreScript.scoreValue);
   
    
 }
   

}

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

3 Replies

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

Answer by dtbrown0801 · Jan 12, 2021 at 07:02 AM

So if I am understanding you correctly you would like to replace

 PlayerPrefs.SetFloat ("Highscore", ScoreScript.scoreValue);

with

 PlayerPrefs.SetFloat ("Highscore",  PlayerPrefs.GetFloat("Highscore", 0) + ScoreScript.scoreValue);
Comment
Add comment · 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
1

Answer by Ashmit2020 · Jan 12, 2021 at 07:54 AM

@dtbrown0801 Thanks for the solution it worked but it also creates one more problem the text that shows how much points the player earned in the match while playing is also saved for example; if the player earned 30 points in the first match then he/she plays second match the scoreboard will start with 30 but it should start with 0. I am thinking that I reset the scoreboard in the start method can you tell me how can I do that and yes the scoreboard has its on separate script that increase score. here it is:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class ScoreScript : MonoBehaviour {

 public static int scoreValue = 0;
 public static Text Score;
 
 // Start is called before the first frame update
 void Start()
 {
    
     Score = GetComponent<Text> ();

     

 }

 // Update is called once per frame
 void Update()
 {
     Score.text = "Score: " + scoreValue;

 }

 

 

}

Comment
Add comment · Show 9 · 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 dtbrown0801 · Jan 12, 2021 at 07:58 AM 1
Share

At the end of the Die $$anonymous$$ethod you could put: ScoreScript.scoreValue = 0;

avatar image Ashmit2020 dtbrown0801 · Jan 12, 2021 at 09:00 AM 0
Share

That might be a good idea but in my case i also have to show the total score that the player earned in game over menu. So if I set the Value to zero in the die method the overall display of the player earned will also change to zero in game over panel is there any way from which I can declare the overall earned points and then change Scorescript.scoreValue() to 0?

avatar image dtbrown0801 Ashmit2020 · Jan 12, 2021 at 09:03 AM 0
Share

Do you have a method that hides the game over menu and resets the match? because that would be the best spot to reset the score

Show more comments
avatar image dtbrown0801 · Jan 13, 2021 at 12:13 AM 0
Share

I think your error is because you need to add .text to the end of script.Score. So it makes script.Score.text = ScoreScript.scoreValue It is also good practice to convert to string as this can sometimes cause errors but I don't think it would in this use case.

avatar image Ashmit2020 dtbrown0801 · Jan 13, 2021 at 10:25 AM 0
Share

Thankyou so much you are a Live-Saver. @dtbrown0801

avatar image
1

Answer by lvskiprof · Jan 12, 2021 at 08:54 PM

If you maintain the score in a class that is implemented as a Singleton, you will be able to access it from your game over menu, assuming that is a different scene.

You can convert your integer value into a string using the ToString() method on the score value:
scoreValue.ToString()

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

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

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

Double Score Problem ? 1 Answer

How to fix this Score UI problem 2 Answers

Score counter breaking after adding points 2 Answers

how to make a shop system 1 Answer

shop script not working 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