P
P
Pragma Games2021-07-23 14:42:19
Unity
Pragma Games, 2021-07-23 14:42:19

How to get the component of a child object?

There is a hierarchy of objects, the script weighs the root object, how you can get an object component from it that is lower in the hierarchy, knowing the name of the desired object. For example, there is a root empty tank object with a script describing its actions, other objects are nested in it: tower, hull, etc. And objects are nested in the tower object: a cannon and a machine gun. How to get the machine gun's transforn without resorting to findobject ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
s7on1250, 2021-07-23
@PragmaGames

You can do this with the Transform.GetChild(index) function
https://docs.unity3d.com/ScriptReference/Transform...

T
Tosha43, 2021-07-27
@Tosha43

GetComponentInChildren() if there is only one component and GetComponentsInChildren if there are several

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question