Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Destroy on collision.

Discussion in 'Scripting' started by g_mister, May 15, 2015.

  1. g_mister

    g_mister

    Joined:
    Dec 5, 2012
    Posts:
    12
    Hello everyone,

    I haven't been able to work out this problem so I've come here looking for help:

    Here is my code that's attached to my bricks:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Bricks : MonoBehaviour {
    5.  
    6.     void OnCollisionEnter(Collision other)
    7.     {
    8.         Destroy(gameObject);
    9.     }
    10. }
    11.  
    What is supposed to happen is the bricks should disappear when the player ball collides with them but this isn't happening.

    What am I missing?

    Any help is appreciated, thanks!
     
  2. Nitugard

    Nitugard

    Joined:
    May 10, 2015
    Posts:
    341
    You need to Have colliders on your bricks.
    Or if you are using 2d colliders?
     
  3. g_mister

    g_mister

    Joined:
    Dec 5, 2012
    Posts:
    12
    Thanks for the reply OneDragutin, I do have colliders as well as the script attached to the bricks:

     
  4. Nitugard

    Nitugard

    Joined:
    May 10, 2015
    Posts:
    341
    As you can see you are using 2D collider, but in your script you are using OnCollisionEnter which is for 3D colliders only!
    To fix this you just need to add 2D at the very end of the function name:
    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D coll) {
    2.         Destroy(gameObject);
    3. }
    Hope this helps!
     
  5. g_mister

    g_mister

    Joined:
    Dec 5, 2012
    Posts:
    12
    Wow that helps a bunch! It's always the little things.

    Thanks OneDragutin!