Need help implementing level counter

I am extremely new to Unity and would love some help getting a level counter put into my game. Basically, I am only trying to display the level the player is on in the game. This is what I have written so far, and though the debug.logs are triggering, the text on the game is not changing (and yes I have assigned the text variable in the inspector). UPDATE: the levelUpdate method is triggering every time the scene changes.

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

//public Text level = SceneManager.GetActiveScene().buildIndex.ToString("0");


public class levelCounter : MonoBehaviour {

    //public int levelCount;
    public Text levelText;
  
    private void Start()
    {
        //levelCount = SceneManager.GetActiveScene().buildIndex;
    }
    // Update is called once per frame
    public void levelUpdate ()
    {
        //levelCount++;
        Debug.Log("Triggered");
        int buildIndex = SceneManager.GetActiveScene().buildIndex;
        levelText.text = buildIndex.ToString("0");
        Debug.Log(SceneManager.GetActiveScene().buildIndex);
    }
}

Thanks for any help you are able to provide!

I don’t see how the following would result in anything but a string of “0”, though I’m rusty on my custom string formatting.

levelText.text = buildIndex.ToString("0");

Just try without the custom formatting first, and work from there.

levelText.text = buildIndex.ToString();

Thanks for the quick reply! I figured it out in the past twenty minutes, I declared the integer buildIndex in the start function, and after that, it just started to work! Full working code:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

//public Text level = SceneManager.GetActiveScene().buildIndex.ToString("0");


public class levelCounter : MonoBehaviour {

    public Text levelText;
    public int buildIndex;


    private void Start()
    {
        buildIndex = SceneManager.GetActiveScene().buildIndex;
        Debug.Log(SceneManager.GetActiveScene().buildIndex + 1);
        levelText.text = "Level: " + buildIndex.ToString("0");
    }
   
    public void levelUpdate ()
    {
        Debug.Log("Triggered");
        levelText.text = "Level: " + buildIndex.ToString("0");
        Debug.Log(SceneManager.GetActiveScene().buildIndex+1);
    }
}

It sounds like your going to use this variable through out your game. Just a suggestion, have you considered storing it in Player Prefs or something global so you can access it across the scenes?

I attached it to an object that is a child of my UI canvas that is a prefab used throughout the game. Is that the same thing?