How to change button text via script?

I know this has been answered before but I have tried everything that other answers have given for the past few days without any success. I’m hoping someone can explain my fundamental misunderstanding behind this.

Here is the current iteration of my script. I’ve clicked and dragged a script into the Globals variable and I’ve clicked and dragged a button in to the ThisButton variable. The rest is obvious. I don’t think I need the import UnityEngine.UI; but I’ve tried without it as well.

#pragma strict
import UnityEngine.UI;

var Globals : GlobalValues;
var ThisButton : UnityEngine.UI.Button;
var CrewMember = 0;

function Start () {

}

function Update () {
	
	if (Globals.Crew1[0] == 1){
		ThisButton.GetComponentsInChildren.<Text>.text = "testing";
	}

}

Right now the error is that ‘UnityEngine.Component.GetComponentsInChildren’ is not a generic definition. But there have been plenty of other errors on other tries.

Basically, if my if statement is true, how do I change the text of my button? Thanks for any help on this.

Assuming you do have a Text component in one of the children of ThisButton, here’s how you would fix it:

ThisButton.GetComponentInChildren(Text).text = "testing";

I recommend you to check the API:

And make sure you’re in JS instead of C#.

The API is Component.GetComponentInChildren<>();

// instantiate button from prefab
GameObject go = (GameObject)Instantiate(Resources.Load("DefaultBtn"));
// get button component from game object
Button myBtn = go.GetComponent<Button>();
// get button text component in children and set the text property
myBtn.GetComponentInChildren<Text>().text = "my button text";