Hello. I have a score manager empty in my game & it is used to display the score. I want it so when it hits 1000 Megabytes, it automatically displays 1 Gigabyte instead of Megabytes. Here’s my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ScoreManager : MonoBehaviour
{
public static ScoreManager instance;
[SerializeField] TextMeshProUGUI scoreText;
void Awake() {
instance = this;
}
int score = 0;
// Start is called before the first frame update
void Start()
{
scoreText.text = score.ToString() + " Gigabytes";
}
public void AddPoint() {
score += 1;
scoreText.text = score.ToString() + " Gigabytes";
}
}
Also, how can I persist my manager so it continues to display score & keeps a continous track rather then resetting every level. Many thanks.
Before assigning a string to scoreText.text, you could check the value of score with an if-statement or a switch. If the value is 1000, you divide the score value by 1000.
Use a local float variable in your method to store the result.
Your units (Gigabytes, Megabytes) could be defined in a string array at the top of your class. And in your method, you could define another local variable of type int which holds the index for the string array.
Then you set the values of the two variables based on your rules.
And the last step would be to concatenate both and assign them to scoreText.text.
Hello Nina. I would love if you could share some code to point me in the right direction as I am new and can’t figure much stuff on my own. Mnay thanks.
Nina’s solution is exactly what you are after so can’t see why it wouldn’t work.
Anyway for your second question, the simplest way is to use the DontDestroyOnLoad() method so that it persists between scenes. So you could change your Awake method to something like:
This code works. Thanks a lot Nina. One more question, I’m trying to add a rule by using an else if statement that if score = 1000, display Gigabyte rather than Gigabytes but, it’s giving me an error that the name “value” does not exist in current context. I’ll share my code: