- Home /
Finding child gameobjects with findgameobjectwithtag
So I want to make enemies that spawn throughout a game and I made the script and everything. However, all the animations and stuff, which rely on the enemy's child gameobjects and their proximity to the enemy, get messed up because all the enemy animations end up using the child gameobjects of the first enemy to spawn. Therefore, I need a way to make sure that only the gameobjects that are child objects of the enemy are used. Is there a quick and efficient way to do this?
Answer by Captain_Pineapple · Aug 05, 2018 at 04:53 PM
Hey there,
you could just iterate over all child transforms in your enemys hierarchy and search for the right ones like this. gameobject.GetComponentsInChildren<Transform>()
should come in handy here since it will return you all transforms that are children of the given enemy. Make sure you use gameobject
in lowercase to actually access the enemys gameobject and not the class.
In case your setup for the enemys is always the same so that you are always searching for the same child transforms i'd suggest to manually create a reference for those to safe performance. Simply create a public transform array on your enemy script in the main object and drag and drop all nessecary child transforms in there in the prefab. Then you always only have to access this array of your enemys script.
Hope this helps. In case something was unclear feel free to ask.
Your answer
Follow this Question
Related Questions
How To Find Child Object and set is own criteria?? 3 Answers
How to enable a specific gameobject child. 1 Answer
This does not make sense this.gameObject.transform.Find() why? 2 Answers
Changing Child objects' position 1 Answer
Animating Child Isues 0 Answers