I took a break a while ago and haven’t touched unity in a patch, so I’m trying out the new UI system. I am kind of preferring legacy, but I haven’t used the new long enough to determine which I like more. Question below

How do I change the text of UI text? (which is child to my button)?

This is the code I am trying to use for my button

>     > import UnityEngine.UI;
>     > 
>     > var introText : Component[];  
>     > introText = gameObject.GetComponentInChildren(text);
>     > 
>     > var stringy = ["yea1", "yea2",  
>     > "yea3", "yea4" ];
>     > 
>     > public function changeText () { 		
>     > introText = stringy[0];
>     > stringy[0]++;
>     > }

I keep getting the errors

“‘text’ is not a member of ‘UnityEngine.Component’”

“Operator ‘++’ cannot be used with an expression of type ‘String’.”

“‘UnityEngine.UI.Text’ to ‘UnityEngine.Component’.”

" Cannot convert ‘UnityEngine.UI.Text’ to ‘UnityEngine.Component’."

Additional question: should text be a child of the button?

Disclaimer: I don’t know javascript or unity script, I know C#. So I might have made some errors. Still, I believe I should get pretty close.

Making sure the types are correct.

“‘UnityEngine.UI.Text’ to ‘UnityEngine.Component’.” Indeed, a Text is not a list of Component.

  1. The UI components that display text on the screen are of type “Text”, not “text”. So the parameter to GetComponent(s)InChildren() should be “Text” with an uppercase T.

    introText = GetComponentInChildren(Text);

  2. You are asking for Text components, so you can use Text as a type for your introText variable, not the super-generic Component.

    var introText : Text ; // If only one UI Text component.
    var introTexts : Text; // If several.

  3. How many Text components are present in your scene? If I understand your situation, only one text is shown at a given time, so there is only one text component.

    var introText : Text;
    introText = gameObject.GetComponentInChildren(Text);

If you have several Text components in the scene, you can do

var introTexts : Text[];
introTexts = gameObject.GetComponentsInChildren(Text);

Note the “s” in “GetComponent s InChildren” here.

Assigning a string to a Text component.

“‘text’ is not a member of ‘UnityEngine.Component’” Indeed, but it is a member of Text.

A Text component has many properties: rectTransform, font, size, alignment, and of course the string to display. You wish to change that string, which you can do with the “text” property of the Text component.

introText.text = "Yea";

Doing

introText = "Yea";

cannot work, as introText is a Text (a fancy UI component in all its complexity) while “Yea” is just a string.

If you have several text components, then you need to specify with which one you are working.

introTexts[0].text = "First line of text."
introTexts[2].text = "Third line of text."

Selecting the string to display.

“Operator ‘++’ cannot be used with an expression of type ‘String’.”

The line

stringy[0]++;

is weird. It means “Take the first element of stringy and increment it”. The first element of stringy is the string “yea1”, and you cannot increment a string. I believe that you wish to display the next string instead.

If there is a single Text component in your scene, then you have to choose which element of stringy you wish to display.

introText.text = stringy*; // Where i is an integer number.*

If you have several components, maybe you want to display everything?
for (var i = 0 ; i < 4 ; i++) { // 4 if you have 4 lines of text to show.
introTexts_.text = stringy*;
}
Child of button.
Does not matter, you can have text anywhere you want.
Changing the text.
You need your script to remember what it is displaying now.
var messageIndex = 0;
var messages = [“I like rabbits”, “Feed me chocolate”, “Time to watch Star Trek”];
var introText : Text;
function Start() {
introText = GetComponentInChildren(Text); // Assuming only one Text component in the button.
introText.text = messages[messageIndex]; // We’ve got to start with something.
}
public function ShowNextMessage() {
messageIndex++; // We will display the next message.
if (messageIndex > messages.length) {
// Unless there is no next message.
messageIndex = 0; // Start from the beginning again.
}
introText.text = messages[messageIndex];
}*
You can attach this script to your button and link ShowNextMessage to the onClick handler of the button._

http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-text

I think this video is what you’re looking for. It tells you how you can change the text via a script

Replace text in your find in children with Text

Uppercase T is important, then set that with

introText.text = stringy…

Cant find square brackets on my phone!

Text is the component and Text.text is the actual string that gets displayed.