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.
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.
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).