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!

2 Answers

2

hi @BoyanaB – GetComponentsInChildren returns an array… GetComponentInChildren (with no s) returns a single value.

Yeah... I read about it too... But seems this doesn't apply to var.Lenght (array) var.Count (List).

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.

Thanks for your help, I´ve already fixed it :)