How do I display the editor text?

I’m wondering- what if I attach the script from Lecture 30 but then decide I want to display the text I first entered in the Unity editor? I suppose the easiest way is to just remove the script from the text object, but how would I call the original text using C#?

Hi @Jorin_Gedamke,

Could you explain a little bit more, in detail, as to what it is you want to achieve please?

Here goes, and be patient with me if I get the Unity terms confused. I was working on an existing project rather than starting fresh for Text 101. I created a text object and typed “Welcome to Jumpbot!” and then wrote the script.

Now what if I want to display “Welcome to Jumpbot!” instead of, or before, the text substituted by the TextController script? Hope this helps.

Hi,

So if I understand correctly, you have one UI Tect game object in your scene.

The text variable has been set, perhaps in the Inspector, or perhaps in code(?), and you want to know how tonadd more text to that UI Text game object?

Assuming I’ve understood correctly…

If it is merely to keep the welcome text as a perminent feature, you could simply add a second UI Text game object and call it “Title” for example. Then reference the other UI Text game object in your TextController script to change its text.

If you only want one UI Text game object then you will need to chamge its text variable via code.

Within your TextController script you will want a class level variable which can be used to hold a reference to the UI Text game object you want to change.

Within the Start() method of your TextController script you will want to add a Find statement to get a reference to the UI Text game object and hold it in your variable. Note, you could do away with this if you are happy to make the above variable public and then drag the game object into this exposed public variable within the Inspector.

Then you just need a method to update the text variable of your UI Text game object as and when needed.


Example using the GameObject.Find() method;

using UnityEngine;
using UnityEngine.UI;

public class TextController : MonoBehaviour
{
    private Text _displayText;

    private void Start()
    {
        _displayText = GameObject.Find("DisplayText");    // assumes your UI Text game object is called "DisplayText"

        UpdateText();
    }

    private void UpdateText()
    {
        _displayText.text = "Hello Jorin!";
    }
}

Example using an exposed public class level variable;

using UnityEngine;
using UnityEngine.UI;

public class TextController : MonoBehaviour
{
    public Text _displayText;

    private void Start()
    {
        UpdateText();
    }

    private void UpdateText()
    {
        _displayText.text = "Hello Jorin!";
    }
}

In the above example, you will need to drag the DisplayText UI Text game object into the exposed public field on the TextController component.


I wasn’t absolutely certain with regards to you query when you mentioned about “instead of / or before”, so to cover all bases…

You can use string concatenation to manipulate the text before passing it to the UI Text game object. For example;

using UnityEngine;
using UnityEngine.UI;

public class TextController : MonoBehaviour
{
    private Text _displayText;

    private void Start()
    {
        _displayText = GameObject.Find("DisplayText");    // assumes your UI Text game object is called "DisplayText"

        UpdateText();
    }

    private void UpdateText()
    {
        string words;    // declares a string variable called "words"

        words = "Hello ";    // initialises the variable with a value
        words += " Jorin!";    // concatenates the string "Jorin" to the value contains in words

        // the += is just shorthand for saying `words = words + " Jorin!";`
        // words now has a value of "Hello Jorin!"

        _displayText.text = words;
    }
}

So, with regards to what I think is title text, you could have this;

using UnityEngine;
using UnityEngine.UI;

public class TextController : MonoBehaviour
{
    private Text _displayText;

    private void Start()
    {
        _displayText = GameObject.Find("DisplayText");    // assumes your UI Text game object is called "DisplayText"

        UpdateText();
    }

    private void UpdateText()
    {
        string title = "Welcome to Jumpbot!";
        string crlf = "\n\n";
        string moreText = "Here is another piece of text that we have added.";

        string concatenatedText = title + crlf + moreText;

        _displayText.text = concatenatedText;
    }
}

In case you are wondering, the _ character doesn’t do anything special, it’s just a naming convention I tend to use for any class level variables in my code. :slight_smile:

Privacy & Terms