-2
\$\begingroup\$

I have a strange problem... I need a 2D projectile to destroy itself after colliding with an object. I have used all of the different ways to do this on the internet but to no avail; the object always collides with the way but is never destroyed! This is my code:

public void OnCollisionEnter(Collision2D other)
{
    Destroy(gameObject);
}

The projectile is a prefab I made and it's attached to a controller I made for the main character. The console is giving me the error, "This message parameter has to be of type: " ... and I'm not sure what type is " ".

The projectile has a rigidBody2D attached to it and a circle collider. The wall is an empty object with an edge collider. I know that there is a collision because the projectile hits the wall and rolls down it. However, my function "public void OnCollisionEnter(Collision2D other)" is never activated, and I don't know why. I'm not sure what you mean by framework... I'm totally new to unity and programming.

When the object hits the wall it simply rolls down it and lands on the ground.

\$\endgroup\$
  • \$\begingroup\$ You should edit your question and describe what is the framework you're using. We need more details. \$\endgroup\$ – Alexandre Vaillancourt Feb 4 '16 at 4:09
  • \$\begingroup\$ I agree with @AlexandreVaillancourt. It's hard to help with so few details. And by the way, you mean that no collision is being detected or that it is being detected but the resolution (i.e. destroying the object) is the part that is not working? \$\endgroup\$ – MAnd Feb 4 '16 at 4:12
  • 1
    \$\begingroup\$ Unity is your framework. This site is not Unity answers, it's a general question and answer site for Game Development (where Unity related question are welcome). This means that you can't assume we know what you're talking about if you don't mention you work with Unity (or anything else). Since mind reading via the Internet is not possible, you must describe your issue thoroughly. \$\endgroup\$ – Alexandre Vaillancourt Feb 4 '16 at 4:27
  • \$\begingroup\$ Generally @AlexandreVaillancourt is right about trying to avoid delete this; but from context (and now the recent edits) this is Unity. Unity's Destroy() method just marks the entity for destruction, with the actual deletion occurring in a batch later in the frame, so it already handles this case for us, and this pattern of Destroy() use is safe. \$\endgroup\$ – DMGregory Feb 4 '16 at 4:30
5
\$\begingroup\$

You want the 2D version of the method: OnCollisionEnter2D

Collisions with a Collider2D/Rigidbody2D won't send the "OnCollisionEnter" message, only "OnCollisionEnter2D"

\$\endgroup\$
  • \$\begingroup\$ EXCELLENT! I've been surfing the web for at least an hour and a half and here is the answer, and it's so simple I feel embarrassed. Thank you! \$\endgroup\$ – John Miller Feb 4 '16 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.