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.
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.
There are many ways.
You could set up a public reference in the inspector instead of using GetComponent:
public Text mText;
Almost the samething as method 1, but use a serialized private reference:
[SerializeField]
private Text mText;
Or if the text component has a unique name, then you could do a search for it:
Text mText = GameObject.Find(“MyUniqueTextName”).GetComponent();
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().GetComponent();
Let the component set itself to the parent:
GetComponentInParent().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.
Зашел в поисках ответа, но все это меня не устроило и вот решение которое мне действительно подошло
GameObject.transform.GetChild(0).GetComponentInChildren().text;
GetChild(0) - здесь мы обращаемся к Дочернему предмету под номером 1 в списке иерархии, изменение цифры дает возможность выбрать нам конкретный Дочерний объект.