- Home /
How do I make a collision detector for the parent which effects the children?
Hello!
I am making this space exploration game, where the player flies around trying not to run into asteroids. I have all the asteroids as a child of Enemy.
The problem is that I cannot figure out how to make my code only work with the children of enemy, not other objects in the game.
I would appreciate any help you can give, thank you!
Here is the code I am using right now, and I know why this doesn't work but I cannot find a solution.
public class Collision : MonoBehaviour {
private void OnCollisionEnter2D(Collision2D collision) {
Application.LoadLevel("Lose");
}
}
Near duplicate of this other question you asked. Your answer is in that question..
https://answers.unity.com/questions/1446176/referencing-a-child.html
Answer by MezFo · Dec 24, 2017 at 06:26 AM
@Chessnutter You need to check what you are colliding with. The game object that you have collided with is stored in "collision". You will need to add a tag called "Enemy" Or whatever you want to call it and give all the astroids objects that tag(done at top of inspector when astriod is selected.). Then you can check if the object stored in collision has that tag. Like this -
private void OnCollisionEnter2d(Collision2d collision)
{
if(collision.gameObject.tag == "Enemy")
{
Application.LoadLevel("Lose");
}
}
Hope this makes sense. Please remember to mark my answer as correct if it does :)