Home>

I want to use TextMesh Pro of Canvas to write Level such as Level1, level2 ...
I want to get the buildIndex for each scene in the script and change the number in the Level 〇 part.
I got a null error in the part that gets the Text. I would appreciate it if you could tell me how to solve it.


The buildIndex has been obtained.
It seems that the acquisition of the Text I want to change is null.
(Script levelText = this.GetComponent();)

Corresponding source code
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class UIManager: MonoBehaviour
{
    private Text levelText;
    // public delegate void SetCurrentState (GameState state);// Type
    // public SetCurrentState setCurrentState;// Function types that can be registered externally
    // Start is called before the first frame update
    void Start ()
    {
        changeLevel ();
    }
    // Update is called once per frame
    void Update ()
    {
    }
    public void changeLevel ()
    {
        // Initialize each scene using index (I want to use it in the Level notation of UiManager)
        int index = SceneManager.GetActiveScene (). buildIndex;
        index ++;
        Debug.Log (index);
        levelText = this.GetComponent<Text>();
        Debug.Log (levelText);
        levelText.text = "Level" + index;
    }
}
Supplementary information (FW/tool version, etc.)

UIManager.cs is attached to the Text (TMP) body.

  • Answer # 1

    TextMeshPro is not Text, so I can't get a reference.

    using TMPro;Load TextMesh Pro with
    GetComponent(), The text for the UIGetComponent()Please refer to it

    reference
    https://unity-mantario.hatenablog.com/entry/2018/07/15/205727