My UI Score Is Not Working?

Okay, when it comes to programming, I am a noob. I usually use Javascript even though I don’t know jack squat about it, but I’ve sort of learnt how it works with the if statements and variables and all, but when I tried C#, which I was terrified of using, it went exactly how I thought it would.

So, I’m trying to get the UI working for my game, a cartoony-bloody border with score and high score text in the top left corner. I have a functioning GUI, but it looks ugly and I know I can do better. So I’m trying to set up the UI to display the score. The C# tutorial I watched had some other score system going on, there wall a ball hitting a rope and the score system was all in the one script, but I could make out some of the script and thought I could set the score to display a PlayerPref int whatever it’s called.

But as you can see in the images below (the maximum is 2, so I put them all in 1) it didn’t work. I’ve included the other scripts involving score.

So maybe someone could help me out? Also, the game involves driving a car around and running over pedestrians, just letting you know so you understand the gameplay I’m going for. And as an extra, could someone tell me how to get a countdown timer in the UI as well? For 60 seconds, BTW.

Thank you! :smiley:

TLDR Version: Used C# for the first time, score UI doesn’t work.

First, assign a gameobject with a Text component on it to the scoreText variable (drag it from hierarchy to the inspector, in Unity), it should work

I think you should avoid using PlayerPrefs in Update / FixedUpdate/OnGUI or everything that is executed every frame

In scoreUI, you get the “yourScore” value from playerpref every Update (every frame)
You should use the currentScore variable in scoreKeeper.js
Same in OnGUI , replace the PlayerPrefs.GetInt(…) by highScore.


For the countdown you can add a float variable somewhere, let say scoreUI.cs (btw, why do you use both cs and js ?), set it to 60 in the Start function and in update, add

countDown -= Time.deltaTime;

deltaTime is the time in second between the last frame and the current one.
Then make a timeText variable which will contains the text that display the time , and update it in Update
It should looks like this

public class scoreUI : MonoBehaviour
{

    public Text scoreText, timeText;
    public float timeLeft;

    void Start()
    {
        timeLeft = 0;
        if (scoreText || timeText)
            Debug.LogError("Assign Text gameobjects in the inspector");
    }

    void Update()
    {
        scoreText.text = "Score: " + scoreKeeper.currentScore;
        timeText.text = "Time: " + timeLeft;
        timeLeft -= Time.deltaTime;
    }
}