3
\$\begingroup\$

So I was trying to make a UI element to keep score in a unity (4.6) project and I'm having trouble assigning the 'Text' element to the Text variable.

So I have the text element with the script attached to it. At the moment the script is empty except from some variables being created, here it is:

using UnityEngine;
using UnityEngine.UI;

public class TextController : MonoBehaviour {

public Text text;
public int score1, score2;

// Use this for initialization
void Start () {
}

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

In the unity UI the Text variable shows but it is empty, and it will not let me drag the 'Text' object from the hierarchy into the variable box. It shows the outline like its going to accept it but remaing 'None (Text)' when I release the click. I tried importing an already working UI from another project into this but when I added it to the hierarchy it removed the text element again (just for reference the text worked perfectly in the other project).

Script element of text

Also a screenshot of the hierarch just in case:

enter image description here

Is there a problem with the scipt or the prject or have I messed something else up?

Edit:

Clicking the circle next to the text variable brings up the window to select the element. Mine shows up without anything in it. I expect it to show the text object I have made.

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

So long as your 'Text' object contains a Text component, this will work as expected. enter image description here

You may have replaced the Text component with your TextController script, or it was removed some other way.

Alternatively, you could change your script as follows:

using UnityEngine;
using UnityEngine.UI;

public class TextController : MonoBehaviour {

    public GameObject textObject;
    Text text;
    public int score1, score2;

    // Use this for initialization
    void Start () {
        if(textObject == null)
        {
            Debug.Log("You must set textObject to an UI component with a Text script");
            this.enabled = false;
            return;
        }
        text = textObject.GetComponent<Text>();
        if(text == null)
        {
            Debug.Log(textObject.name + " doesn't contain a Text component!");
            this.enabled = false;
            return;
        }
    }

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

This way, you can just drag the GameObject, and the script will retrieve the Text component on Start(). If the Text component is not available, the script will warn the developer and disable itself.

\$\endgroup\$
6
  • \$\begingroup\$ It still had the text component attached but it still would not allow me to. I will try the other method though, thanks! \$\endgroup\$ Commented Jun 23, 2016 at 11:03
  • \$\begingroup\$ I'm setting the variable 'text' the Text component of the text object, however it still will not let me change the displayed text. I tried using 'text.guiText.text = "a" ' but it is not throwing any errors or displaying the text. \$\endgroup\$ Commented Jun 23, 2016 at 16:57
  • \$\begingroup\$ It's just 'Text.text' \$\endgroup\$
    – House
    Commented Jun 23, 2016 at 18:33
  • \$\begingroup\$ It says "Text does not contain a definition for text" \$\endgroup\$ Commented Jun 23, 2016 at 20:58
  • \$\begingroup\$ Alright, I guess I'd have to install 4.6 to get a real solid answer. I admit my answer is only tested on 5.0+. (You might consider upgrading?) \$\endgroup\$
    – House
    Commented Jun 24, 2016 at 1:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .