Clicking one prefab effects them all.

Hi. I know this is simple, but I couldn’t find an answer here or elsewhere online.

I have a prefab called “cuboid” which has been copied a number of times. I’m detecting a mouse click using raycast, then printing to the debug log. The issue is that when one object is clicked, all instances print to the debug log, not just the one clicked.

The script is on each instance of the prefab:

#pragma strict
function Update () 
	if (Input.GetMouseButtonDown(0)) 
		var hit: RaycastHit;
		var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		if (Physics.Raycast(ray, hit)) 
			if ( == "cuboid" )Debug.Log( "Object was clicked");

Can someone please indicate why this is happening and how to correct it?


Have you considered using OnMouseDown or OnMouseUp?

Saves bothering with a raycast and should only trigger on the cuboid under the mouse.

Also works for touch devices.

This post contains the answer to the question, for anyone who encounters it in future: