how to find which object is clicked

Hi all,

am having one cube object and have attached move script to cube.I have added this cube as prefab.
Dynamically am creating object for cube prefab. Onmouse click i have to find out which cube i have clicked.

if(Input.GetMouseButton(0)||Input.GetMouseButton(1))
{

var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray, hit))
{
var hitobjname=hit.collider.gameObject.name;
if(hitobjname==“Cube(Clone)”)
{
print(“action…”);
}
}
}

The above code is always entering into loop without touch the cube also…

How to do the best way…

Thanks

Try to use this:
if(Physics.Raycast(ray, hit))
{
if(hit.transform.tag==“Cube”)
{
print(“action”);
}
}

You need to assign the tag name “Cube” to the cube object. :slight_smile:

You can also use the OnMouseDown/OnMouseUp/OnMouseEnter/etc methods. (I don’t think these work on iPhone though)

Correct - these do not work in iPhone.

Hi,

May i know the solutions?..

There’s a thread here that explains how you can detect the object under the mouse using a raycast from the camera.