Search Unity

[Help] Click to Destroy Object (2D)

Discussion in 'Scripting' started by RandomActStudios, Jul 24, 2014.

  1. RandomActStudios

    RandomActStudios

    Joined:
    Jul 21, 2014
    Posts:
    15
    I have created two spawners that emit enemies and I am trying to make it so that when I left click these enemies they get destroyed and even eventually play a death animation.
    I tried running a Debug.Log but it's not even picking up that I'm left clicking.
    Does anyone know what I'm doing wrong? Is this not supposed to be attached to the object I'm destroying?
    Here is what I have...
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function OnMouseDown() {
    4.     Destroy(gameObject);
    5.     Debug.Log (" Click ");
    6. }
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    I recommend using raycasting.

    You can cast a ray from the main camera at the mouse position and obtain information from the GameObject through RayCastHit.

    Check out my signature for a video that goes over raycasting.


    Best,

    Jon