- Home /
OnCollisionEnter: Collision takes also the parent tag
Hey everybody,
I have a problem with the methode CollisionEnter(Collision col). I have a player and if an enemy collides with him the game is over. A shield should protect the player a few secounds from colliding with the player. The shield object is a child of the player, but if the enemy hits the shield its also game over, although the shield hasnt the tag Player.
What can I do? Thank you for help, here is my code and a piece of my hierachy:
void OnCollisionEnter (Collision col)
{
collisioncount++;
if (col.transform.tag == "Player") {
print ("Game over because hit enemy");
Guiandco go = guiforgo.GetComponent<Guiandco> ();
go.SendMessage ("GameOver", SendMessageOptions.DontRequireReceiver);
}
}
$$anonymous$$aybe the shield collided is too thin and either enemy collided clips through the shield or the player collided clips through the shield? Check that, then maybe your code for the shield isn't working and is disabled straight away when shield is touched?
Answer by Tonyx97 · Aug 24, 2014 at 10:28 AM
Instead of this col.transform.tag == "Player"
you can use better this if (col.transform.GetChild(0).name != "shield")
this code says if the col hit (player) has a child called shield on the index number 0 then will not game over.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Problem with child and parent. 1 Answer
Simulate 'Child' Effect by Script 2 Answers
Problem with finding compnent of immidate children 1 Answer
How can I listen for a function being called from a parent class? 1 Answer