[SOLVED] How to Change Text in C#?

So I just made the leap from being a JS newbie to a true C# programmer, but I have one issue:

How do I change UI text in C#?

In JS, you’d make a variable of type UI.Text

But apparently that doesn’t work in C#?

I get an error saying:

“Error CS0246: The type or namespace name ‘UI’ could not be found. Are you missing an assembly reference?”

I thought maybe to include “using UnityEngine.UI” but that didn’t change things.

I don’t know what to do here. How do I make a UI.Text variable?

Any help would be greatly appreciated!

- Chris

EDIT: Found the solution with a different Google search than what I tried before and got this. So as I thought, "using UnityEngine.UI;"I is required, but then the variable type is simply Text, not UI.Text.

Hope this answer helps anyone who comes across this issue!

You do need a using UnityEngine.UI; and then declare the text like

using UnityEngine.UI;

public class MyClass : MonoBehaviour
{
  public Text myText;

  public void setText(string newText)
  {
    myText.text = newText;
  }
}

I strongly suggest you step over to Learn and jump through some c# based tutorials - they’ll help out a lot.

1 Like
start 
WhatYouWant = textGO.GetComponent<Text>();

public Text WhatYouWant;

public GameObject textGO;

textGo.text = WhatYouWant;
1 Like

Both of you replied right after I figured it out :slight_smile: Ty for the help!

2 Likes

For the record, you could also write out the fully qualified name, as in UnityEngine.UI.Text. It doesn’t make much sense for UI, but I frequently use it when I just want to get a single type, and am not interested in the rest of the namespace.