I would like my Score Manager to update

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.

Hi Zunaim,

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.

Did this help?


See also:

1 Like

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.

Actually, I do not have time to write custom solutions but here this is how you could write your code in a very simple straight-forward way:

if (score >= 1000)
{
    float value = score / 1000;
    scoreText.text = value.ToString() + " Gigabytes";
}
else
{
    scoreText.text = score.ToString() + " Megabytes";
}

And if you want to add more rules, you simply add more else-ifs.

Thank you very much Nina for the reply. I’ll test this piece of code & then update you accordingly.

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:

    void Awake() {
        if (instance == null) {
            instance = this;
            DontDestroyOnLoad(this.gameObject);
        }
        else {
            Destroy(this.gameObject);
        }
    }

Thanks for the reply. Unfortunately, the score manager breaks once the player dies. This solution is not working.

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:

void Update() {

       

        if (score >= 1000)

        {

            float value = score / 1000;

            scoreText.text = value.ToString() + " Gigabytes";

        }

        else if (score = 1000) {

            scoreText.text = value.ToString() + " Gigabyte";

        }

        else

        {

            scoreText.text = score.ToString() + " Megabytes";

        }

    }

Not put the score manager inside or under the player?

(have a dedicated/separate game object for the ScoreManager)

A comparison is made with ==.

= assigns a value to a variable. That operation is invalid in an if-condition. See your else-if condition.

I have a separate Game Object for the Score Manager.

I’ve added an == to the else-if statement but it still shows the same error.

Double click on the error message. To which line does it refer? Could you paste it here?

It’s referencing the line under the else-if statement. I’ll paste it here:

        else if (score == 1000) {
            scoreText.text = value.ToString() + " Gigabyte";
        }

Where is value defined in the else if block? Take a closer look at my answer.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms