- Home /
Accessing text in GUIText Component: Slider.GetComponentsInChildren().text is invalid
Hi all,
I have a Slider GameObject with a GUIText child. I am writing a C# script that changes the text component in the GUIText, but am getting the error UnityEngine.GUIText[] does not contain a definition for 'text' of type UnityEngine.GUIText[]
public Slider timeSlider;
int i = 5;
timeSlider.GetComponentsInChildren<GUIText>().text = 'Some Text' + i;
Would anyone know why ().text is invalid here?
Thank you!
Answer by blueblazes63 · Sep 08, 2016 at 04:41 PM
hi @BoyanaB -- GetComponentsInChildren returns an array... GetComponentInChildren (with no s) returns a single value.
Answer by Bunny83 · Sep 08, 2016 at 04:34 PM
You used GetComponentsInChildren while you probably want to use GetComponentInChildren. Note the "s"!
GetComponentsInChildren returns an array, so it returns all components of the given type. GetComponentInChildren returns the first component of the given type it can find.
Your answer