How do I test if a certain object was clicked if multiple objects have the same script

void OnMouseDown ()

	Destroy (gameObject);


I have multiple gameobjects with this script on them and I was wondering how I could check which one I destroyed

You need to check the position of the cursor. If the cursor is within the bounds of the object (or within some short of bounds / handle) then destroy, otherwise it’s a different object you want to destroy (or none at all). Note that the OnMouseDown() function isn’t checking for if the mouse was clicked on this object, it’s just a method that is raised whenever the mouse is clicked, where-ever it is clicked.

If you’re working within 2d, you might be able to use the RectTransformUtility to determine if the click is valid. If not then you will need to look at a ray from the mouse click position into the world so see what object is “selected”.