- Home /
How do I make a high score based on how many game objects I destroy?
How do I make a high score based on how many game objects I destroy?
Answer by Casiell · Jan 22, 2021 at 07:49 AM
You must have some code that is destroying objects. Just count the times you destroyed an object.
I'm not really sure what answer are you expecting here?
Answer by Matt_2001 · Jan 22, 2021 at 08:02 AM
To make a highscore system and to add score to it I suggest using playerprefs but they have their own pros and cons like it can be easily editable by the user and it is not that secure but you can use it for small data like these for more information you can see this post PlayerPrefs unitydocs Ok now to make that system work you can add add score code in the method that responsible for destroy like this: ScoreScript.scoreValue += 10;
and you also have to make a score script like this: 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;
}
}
and to keep track of the score you can do like this: PlayerPrefs.SetFloat ("Highscore", PlayerPrefs.GetFloat("Highscore", 0) + ScoreScript.scoreValue);
(this will add the score) and this will overwrite the score PlayerPrefs.SetFloat("Highscore'', ScoreScript.scoreValue);
and you can display the score in ui like this: scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
I hope this will help Thanks,
Your answer
![](https://koobas.hobune.stream/wayback/20220613023754im_/https://answers.unity.com/themes/thub/images/avi.jpg)