Pick a certain instance through Raycast2D


I have a bunch of clones and I would like to detect one though Raycast.

Here is my code:

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

				if (hit != null && hit.collider.gameObject.GetInstanceID () == this.GetInstanceID ()) { ...

When I’m over this instance, not only this one, but all clones of same gameobject are affected.

Many thank yous.

I’m not sure what is going on with your code. There is a problem here, but it should result in your id check always failing. You code:

hit.collider.gameObject.GetInstanceID () == this.GetInstanceID ()

This is comparing the instance id of the game object to the instance id of the script/component. You can do instead:

hit.collider.gameObject.GetInstanceID () == gameObject.GetInstanceID ()

Or even simpler:

hit.collider.gameObject == gameObject