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.
-
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);
-
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.
-
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._