11

I am trying to delete the object on which the mouse is clicked. I am making a 2D game using the new Unity3D 4.3. Here is the code I'm using

void Update () {

    if (Input.GetMouseButtonDown(0)) 
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if(Physics.Raycast(ray,out hit))
        {
            isHit = false;
            Destroy(GameObject.Find(hit.collider.gameObject.name));

        }
    }

}

The control is not entering the inner if loop. (isHit is not being set as false).

Bart
  • 19,692
  • 7
  • 68
  • 77
Bimal Bose B S
  • 221
  • 1
  • 5
  • 14
  • 1
    Does the object have a collider? Does it have a collider and a rigidbody? – Happy Apple Dec 14 '13 at 13:09
  • Yes it contains a collider and a rigidbody – Bimal Bose B S Dec 14 '13 at 14:27
  • 3
    I used Physics2D and it works!! This is the modified code Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Vector2 orgin = new Vector2(ray.origin.x,ray.origin.y); RaycastHit2D hit = Physics2D.Linecast(orgin,-Vector2.up,1 << LayerMask.NameToLayer("Supports")); Destroy(hit.collider.gameObject); But now the when even when i click on other places the object gets deleted.. I think the problem is with the raycast direction being -Vector2.up.. Which direction should i cast it to ?? – Bimal Bose B S Dec 14 '13 at 14:28
  • not really part of what you asked, but in this line: `Destroy(GameObject.Find(hit.collider.gameObject.name));` find is not needed. Simply doing `Destroy(hit.collider.gameObject);` should work just fine. – Steven Mills Dec 16 '13 at 07:53
  • I can't use that condition due to error. It said, "cannot convert from 'UnityEngine.Ray to UnityEngine.vector2'." Same as Ray ray. – David Dimalanta Feb 12 '14 at 03:03

4 Answers4

19

You cannot use 3D physics functions on the new 2D stuff. Use the 2D functions instead. Example:

RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

if(hit.collider != null)
{
    Debug.Log ("Target Position: " + hit.collider.gameObject.transform.position);
}
Esa
  • 1,636
  • 3
  • 19
  • 23
8

This question is a bit old, but I was looking for a a way to get a GameObject with a mouse click in unity 2D, and the Answer from Esa almost helped me, but I couldn't afford to make it to work, so with a bit of research I saw that Camera.main.ScreenToWorldPoint was returning the center of the screen area of the Camera and to it work right. it required to enter the difference in Z position from the camera and the nearest GameObject. My Camera was set by default in -10 and my GameObject was in 0, so all I needed to do is set my Input.mousePosition.z to 10. So if you are getting problem to work with Esa's code (like me :( )the code bellow may help you:

RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10)), Vector2.zero);

if(hit.collider != null)
{
    Debug.Log ("Target Position: " + hit.collider.gameObject.transform.position);
}
6

First attach any type of 2D collider to your GameObject, then pick one of those solutions;

1st Case - If there are more than 1 GameObject on top of each other, and you try to understand specific GameObject is clicked:

void Update ()
{
    if (Input.GetMouseButtonDown (0)) {
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        RaycastHit2D[] hits = Physics2D.GetRayIntersectionAll (ray, Mathf.Infinity);
        foreach (var hit in hits) {
            if (hit.collider.name == name) {
                MyFunction ();
            }
        }
    }
}

2nd Case - If there is only 1 GameObject, and you try to understand if it is clicked:

void Update ()
{
    if (Input.GetMouseButtonDown (0)) {
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        RaycastHit2D hit = Physics2D.GetRayIntersection (ray, Mathf.Infinity);
        if (hit.collider != null && hit.collider.name == name) {
            MyFunction ();
        }
    }
}
2

You've to attach a mesh collider(any collider) with your object first to enter the inner If. Then,

Destroy(hit.collider.gameObject); 

will simply do the job.

There's might be an other work around here.

void Update () {

    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;

    if(Physics.Raycast(ray,out hit))
    {
        if(Input.GetMouseButtonDown(0))
        {
            isHit = false;
            Destroy(hit.collider.gameObject);
        }
    }
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98