- Home /
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);
}
}
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);
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;
}
}
At the end of the Die $$anonymous$$ethod you could put: ScoreScript.scoreValue = 0;
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?
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
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.
Thankyou so much you are a Live-Saver. @dtbrown0801
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()
Your answer
Follow this Question
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