2

I'm trying to add a scoring method in my Unity phone game by changing the UI text. Here is My code:

using UnityEngine;
using UnityEngine.UI;

public class ScoreDisplay : MonoBehaviour
{
    // Start is called before the first frame update
    [SerializeField]private Text m_MyText;

    void Start()
    {
        m_MyText.text = "This is my text";
    }

    // Update is called once per frame
    void Update()
    {
         m_MyText.text = "UWU";   
    }
}

Nothing pops up when I search for a text object:

Nothing pops up when I search for a text object

And I cannot drag the mesh text into the public variable. Any help would be appreciated, thank you.

2
  • Use TMP_Text instead of Text. Commented Jun 13, 2022 at 19:17
  • You are using Text Mesh Pro Text. Include the TMP namespace and use the type TextMeshProUGUI instead of Text. Commented Jun 13, 2022 at 19:19

1 Answer 1

2

Text is a legacy element and is mostly unused. Instead, you want Text Mesh Pro's text component which is under the TMPro namespace. The componant is called TextMeshProUGUI.

Replace

[SerializeField]private Text m_MyText;

with

[SerializeField]private TMPro.TextMeshProUGUI m_MyText;

The TextMeshProUGUI componant has a text field, so your code should work as normal after replacing the line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.