- Home /
Display variable from parent in textbox
I am trying to display a value in text that is taken from a variable in the parent. For example, I have a prefab sprite with an attached script that monitors stats of the sprite such as HP. I would like to have a textbox, which is a child of the sprite, display that value, but I can't figure out how to do this.
What method do you use to read a variable from a parent for use in the child? I've read probably a dozen pages of similar questions and documentation but I just can't seem to get it.
Answer by Qasem2014 · Dec 24, 2014 at 11:24 AM
<< Put This Script On Your Text Object >>
Your Parent :
var Parent:GameObject = transform.parent.gameObject;
Read The Variable And Set It :
GetComponent(Text).text = Parent.GetComponent(YourScript).Variable;
Notice : This Code is For New UI System (Unity v4.6)
Your answer
Follow this Question
Related Questions
Have child ignore parent's tag? 1 Answer
Destroying childs of a specific Parent 1 Answer
Make a simple tree 1 Answer
Move one object with another, without parenting it to the object? 1 Answer