- Home /
How could i access to a specific collider box if it is in a children of the main Object? The script is in the main object.
Im making a 2D game so i want my enemy to receive damage when my player hits, the issue that i have is that i dont know how to call just the collider box that is in a children of the main objet(in this case the enemy), so it can "comparetag" with the weapon of my player.
I was trying with ontriggerenter2D the issue is that i dont know how to only access to the box colliders of a especific children because there are other children colliders that interfere in the process. Any one can help me pls? Here are the image references:
Hi!
Forgive me, but it is difficult for me what you want to accomplish.
Does the "enemy" object have multiple children that have a collider as a component? If so, the script with the "OnTriggerEnter2D" or "OnColliderEnter2D" method must be added to the child you want to capture the action.
If the same script is placed on all children that have a collider, then each child will trigger the same piece of code.
Please explain better what you want to accomplish because the sentence is a bit confusing.
yea thats what i mean, thnks i did it. The problem now is that i want to animate the enemy when its hitted but my animator controller is on the main object. How can i acces to it? Is there a command? the script does not recognize it
Answer by Nistroy · Jun 29, 2021 at 06:35 PM
Hi @DrututtZ ! Simply create a script in which there is the component.
Okey, I tried that and it detects the collider. The problem now is that i want to animate the enemy when its hitted but my animator controller is on the main object. How can i acces to it? Is there a command? the script does not recognize it
You need to access to parent of the collider.. write this into the "if" statement:
Animator animator = collision.transform.parent.GetComponent();
Thanks dude! Now it works perfectly. I did not know the use of "parents"-