I can't print out the text in the game

Hello guys! I am a student and I am new to C# and am currently making my second game using this language.

I want to print out a text where the text is the score and the score is increasing as the time goes by, and I have been figuring out what is the problem with the code.

Thanks,
K

<Preformatted text

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class ScoreManager : MonoBehaviour
{
    #region Singleton
    
    public static ScoreManager Instance;

    [SerializeField] private TextMeshProUGUI scoreUI;

    private void Awake()
    {
        if (Instance == null) Instance = this;
    }

    #endregion

    public float currentScore = 0f;

    public bool isPlaying = false;

    private void Update()
    {
        if (isPlaying)
        {
            currentScore += Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.Space))
        {
            isPlaying = true;
        }
    }

    public void GameOver()
    {
        currentScore = 0f;
    }

    public string PrettyScore ()
    {
        return Mathf.RoundToInt(currentScore).ToString();
    }

    ScoreManager sm;
    private void Start()
    {
        sm = ScoreManager.Instance;
    }

    private void OnUGUI()
    {
        scoreUI.text = sm.PrettyScore();
    }
}

OnUGUI is not a method. Did you mean OnGUI? It wouldn’t make sense to use that either since that’s for immediate mode gui stuff. If you’re just updating a TMP_Text’s text why can’t you just do that in the Update method?

Also, that said, this code seems kind of weird. Why are you making a singleton instance? You should also destroy the object in Awake if Instance is already set since you don’t want two of them. Why are you grabbing the instance and calling methods on it in the same class? You only need to grab the static reference of a singleton in other monobehaviours. Calling sm.PrettyScore() and just calling PrettyScore() in your Update loop would call the same method on the same gameobject. No need to do that here.

I did the method you said simply because I was looking for a tutorial on YouTube and this is how he do it, so I typed the code just like he did but I ran into some problems. Can you suggest a way of how to print out a text that shows the score as the time goes by? Would really appreciate it, also thank you for your reply on my question, it helps me understand what the problem is.

Thanks,
K

Put the same code in Update and it should work. OnGUI was used in the legacy system from before Unity 4.6 days when we got the UGUI system. OnUGUI isn’t one of those built in lifecycle methods and was probably a typo.