- Home /
The question is answered, right answer was accepted
change value of child ui text element
I have a series of dynamically created objects (coming from a prefab) and I need to change the value of a child ui text element on each one just after they are instantiated.
Here is my current code:
                 Transform button = Instantiate(ButtonPrefab);    
                 Text ButtonText = button.GetComponentInChildren (Text);
                 ButtonText.text = "this be some text!";
This code is giving the error 'UnityEngine.UI.Text' is a 'type' but a 'variable' was expected. What am I doing wrong?
Answer by sleepandpancakes · May 31, 2017 at 05:13 PM
You can use GetComponentInChildren either by passing a type or using the generic version, like so:
  //Generic
  Text ButtonText = button.GetComponentInChildren<Text>();
 
 //Typeof
  Text ButtonText = button.GetComponentInChildren (typeof(Text)) as Text;
That's it working now, thanks for knocking me out of a syntax war :)
Follow this Question
Related Questions
How to make a world canvas visible only from front side in a VR game ? 0 Answers
UI camera not matching main camera in VR 0 Answers
Do I need to re-invent the Unity UI system to implement UI in 3D? 1 Answer
Modify a script to use buttons instead of keys 1 Answer
Moving a ScrollRect child via script to focus on some point. 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                