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\$
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.

|improve this answer|||||
\$\endgroup\$
  • \$\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\$ – James Tompkins Jun 23 '16 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\$ – James Tompkins Jun 23 '16 at 16:57
  • \$\begingroup\$ It's just 'Text.text' \$\endgroup\$ – MichaelHouse Jun 23 '16 at 18:33
  • \$\begingroup\$ It says "Text does not contain a definition for text" \$\endgroup\$ – James Tompkins Jun 23 '16 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\$ – MichaelHouse Jun 24 '16 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.