- Home /
How to get a single Text component from one of multiple children all containing Text components
Fairly self explanatory; I'm trying to getcomponent a specific (solitary) Text component from a gameobject containing multiple children, all of which have their own text component.
Any help appreciated.
Answer by Jessespike · Apr 08, 2016 at 07:32 PM
There are many ways.
1) You could set up a public reference in the inspector instead of using GetComponent:
public Text mText;
2) Almost the samething as method 1, but use a serialized private reference:
[SerializeField]
private Text mText;
3) Or if the text component has a unique name, then you could do a search for it:
Text mText = GameObject.Find("MyUniqueTextName").GetComponent<Text>();
4) You could also create a dummy script that you can search for with GetComponent or GetComponentInChildren:
public class UniqueIdentifierSample : MonoBehaviour {
}
then in your script:
Text mText = GetComponentInChildren<UniqueIdentifierSample>().GetComponent<Text>();
5) Let the component set itself to the parent:
GetComponentInParent<MyScript>().mText = this;
There are more ways, such as using a singleton manager class to stores the references, but these examples should be enough. I think the 1st method is probably the easist solution. Just make a public reference and set it up in the inspector.
Couldn't ask for a more comprehensive answer. Thank you.
Answer by StandAloneStudio · Apr 13, 2020 at 11:27 PM
Зашел в поисках ответа, но все это меня не устроило и вот решение которое мне действительно подошло GameObject.transform.GetChild(0).GetComponentInChildren().text;
GetChild(0) - здесь мы обращаемся к Дочернему предмету под номером 1 в списке иерархии, изменение цифры дает возможность выбрать нам конкретный Дочерний объект.
Your answer

Follow this Question
Related Questions
How to Access Components in Children of GameObject? 1 Answer
how to access a float from one script in other script in different objects 3 Answers
How to run a function in a GO that have DontDestroyOnLoad 1 Answer
Best way to assign and work with Canvas 1 Answer
How do I assign a script to an instantiated prefab? 2 Answers