I cannot use the text function in Unity

30 views Asked by At

I tried doing this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Text : MonoBehaviour
{
    public Spawn_stuff spawn_Stuff;
    public Text display;
    // Start is called before the first frame update
    void Start()
    {
        display.text = "Hello world".text
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

But Unity will not recognize the .text or public Text display; as being part of the UnityEngine.UI field. It thinks the syntax is incorrect.

1

There are 1 answers

0
LittleStar On

Let's try to use TextMeshPro to display text in Unity. if you use Text from TextMeshPro in Unity Editor so in script you have to reference TextMeshProUGUI not Text.

here is new version of your script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Text : MonoBehaviour
{
    public Spawn_stuff spawn_Stuff;
    public TextMeshProUGUI display;
    // Start is called before the first frame update
    void Start()
    {
        display.text = "Hello world".text
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}