Mouse events on instantiated prefab don't trigger

I’ve made a prefab with every component added that is needed for mouse events. I added a script component with
an OnMouseEnter() function that changes the color of the object to red.

When I manually add this prefab to the scene the OnMouseEnter event triggers fine and the object turns to red.

Now when I instantiate this prefab per script, the OnMouseEnter event does not fire, but the Update() function in the same script does.

Can someone tell what I’m doing wrong?

Best Regards
René

Are you sure your prefabs have colliders so the OnMouseEnter can be triggered? :slight_smile:

My prefab in the Project view does have a BoxCollider.
If I instantiate this Prefab via script into my scene, the mouse event does not trigger.
If I put the prefab per Drag’n’Drop into my scene the mouse event works fine.

Furthermore, if I click one of the per script instantiated prefab in the scene View, it also shows the BoxCollider in the Inspector.

And I put the following intor the Update() function of the prefab:

	var s = transform.GetComponent("Collider").bounds.size;
	Debug.Log("X:" + s.x + " Y:" + s.y + " Z:" + s.z);

The console prints out the dimensions of the collider object, so the instantiated object has to have a collider, hasn’t it? (Still wondering, why the Update() function gets called but the OnMouse event functions does not.)

Can you post the script you are using? It would be useful to know exactly how you are instantiating the prefab in the script and what you are doing inside the OnMouseEnter function.

Here is the code that instantiates the prefab, where product1 ist the Transform link to my prefab in the Project view.

var product1 : Transform;
var product1_scaleX = 0.3;
var product1_scaleY = 0.4;
var product1_scaleZ = 0.2;
var product1_count = 8;

var product2 : Transform;
var product2_scaleX = 0.3;
var product2_scaleY = 0.4;
var product2_scaleZ = 0.2;
var product2_count = 8;

var product3 : Transform;
var product3_scaleX = 0.3;
var product3_scaleY = 0.4;
var product3_scaleZ = 0.2;
var product3_count = 8;

private var gap = 0.03;

function Start()
{
	LoadProducts();
}

function Update () {
}

function LoadProducts()
{
	var i;
	var p;
	var rSize : Vector3;
	var pSize : Vector3;
	var newPosX;
	var newPosY;
	var newPosZ;
	if( product1 )
	{
		// get dimension of product1

		for(i=1;i<=product1_count;i++)
		{
			p = Instantiate(product1, transform.position, transform.rotation);
			
			// scale new object
			p.transform.localScale = Vector3( product1_scaleX , product1_scaleY , product1_scaleZ );

			// get rag dimensions
			rSize = transform.GetComponent("Collider").bounds.size;
			
			// get product dimensions
			pSize = p.transform.GetComponent("Collider").bounds.size;
			
			// calculate positions
			newPosX = transform.position.x - rSize.x/2 + pSize.x + (pSize.x + gap) * (i-1);
			newPosY = transform.position.y + 0.25 + pSize.y;
			newPosZ = transform.position.z - rSize.z/2 + pSize.z/2 + gap;
			
			// set position
			p.transform.position = Vector3( newPosX , newPosY , newPosZ );
			
			Debug.Log("Produt1 created:" + p.gameObject.name);	
		}
	}

//Now the same for product2 and product3

}

And here ist the code in the prefab itself to detect mouse events:

function Update () {
	Debug.Log("productController::Update");
	// get dimensions
	var s = transform.GetComponent("Collider").bounds.size;
	Debug.Log("X:" + s.x + " Y:" + s.y + " Z:" + s.z);
}

function OnMouseEnter() {
	Debug.Log("productController::OnMouseEnter");
	renderer.material.color = Color.red; 
}

function OnMouseExit() {
	Debug.Log("productController::OnMouseExit");
	renderer.material.color = Color.white; 
}

I want to fill a storage rack with some products prefabs (product1, product2, product3). And the products should be clickable with the mouse.

I hope the code can help you to see what is going wrong. Thanks in advance.

Did you ever find a resolution for this? I’m having the same issue.

What version of Unity do you use?

I’m having the same problem…Using Unity 3 Version 3.2.0f4

i can’t use OnMouseEnter on Instantiated objects. Raycasting works, but i cat figure a mouse Exit version of the raycast.

I have the same problem, and i cant find the solution =/

Hey. I know this is an old thread but I think I might have the solution.

Remove the mouse event function from the prefab script. Create a new empty game object and add a script to that game object. Add the mouse event function to this script and set a reference to the instantiated prefab using “GameObject.FindGameObjectWithTag()”.

This worked for me so hopefully it should work for you (assuming you’re still facing this problem).

I don’t get this… How do you “set a reference to the instantiated prefab using"GameObject.FindGameObjectWithTag()”. "?

Can you please be more elaborate with the code and explanation?