OnMouseUp does not work on instantiated object

Okay,

I seem unable to figure this out nor find an answer so I kindly ask for help.

So I got an object with a simple “OnMouseUp” script that does nothing else than print a “clicked” message.

I have a prefab, drag the object into the scene, press play, and it works just fine. it’s clickable and it prints “clicked” just like it should.

However, if I use the “instantiate” command within a a startup script to automatically load the object it does NOT work. The object loads fine and shows up on the screen and everything, but it is no longer clickable for some reason. It just seems to completely ignore it has a “OnMouseUp” command attached to it.

Adding other things in the object script works fine too, but anything mouse-related, like OnMouseUp, OnMouseOver, etc does not work.

Anyone have any idea why?

Thanks for the answers.

The script is definitely included in the prefab since other portions of the script works fine.

And the prefab is also updated. If I have an empty scene, drag the prefab into the scene and press play it works. If I instantiate the same prefab using a script it does not.

Here is my script on the object I want to click. I have removed everything else for debugging.

function Start () {

    print("SCRIPT IS WORKING");

}

function OnMouseUp () {
    	
    print("Click!");	
}

And thats it. The code in the start function works fine when I instantiate, but the object does not become clickable.

When I press play I run this script to instantiate.

var smallChipTest : Transform;

function Start () {

     var myObjectInstance1 : Transform = Instantiate(myObject, Vector3(0, 0, 0),
     Quaternion.identity);

}

And thats it. I’ve tried using GameObject instead of Transform but it does exactly the same. The code in the Start function works fine, but the OnMouseUp does not. It’s like the OnMouseUp function is completely ignored when instantiating, but when manually adding the object to the scene, its there.

I dont use layering or parenting in any way, as far as I know anyway.

I’m not exactly sure what you are doing as you didn’t provide enough information but I can guarantee you that the OnMouseUp works even if you instantiate the object. Take the following example:

  1. Grab the following c# script and place it on an empty GameObject (ctrl+shift+n). It will give you a button to instantiate a prefab.

    using UnityEngine;
    using System.Collections;

    public class Example : MonoBehaviour
    {
    public GameObject prefab;
    // Use this for initialization
    void Start ()
    {

     }
     
     // Update is called once per frame
     void Update ()
     {
     
     }
     
     void OnGUI()
     {
     	if(GUI.Button(new Rect(0,0, 100, 50), "Instantiate"))
     	{
     		Instantiate(prefab, Vector3.zero, Quaternion.Euler(Vector3.zero));
     	}
     }
    

    }

2)Now create a cube in your scene. Make sure it has a Collider component (it should by default), otherwise the object will not pick up any mouse clicks.

3)Drag the following c# script into that cube you just created:

using UnityEngine;
using System.Collections;

public class ExampleClickableObject : MonoBehaviour
{

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnMouseUp()
	{
		print("this works!");
	}
}

4)Drag this cube from your Hierarchy window to your Project window to create a prefab of it.

5)Now select the empty GameObject that you created in step #1 and drag the cube prefab onto the public prefab property to give the Example script a reference to the prefab.

Now, delete whatever is on screen (which should be the cube you used to create the prefab) and run Unity. Click the Instantiate button and then try clicking the cube. You should see the message “this works!” appear in the console window.

Take a look at what is different between your stuff and mine, and you should be able to figure out what you did wrong. I know you probably already got your answer but I’m posting this in case anybody else runs into the same problem.

There should be two reasons for this to happen:

1 - prefab not updated

you have the object in your scene, which is different from the prefab you use in the instantiate instruction. be sure to create a new prefab using the working object in your scene, and using that new prefab in your script

2 - incorrect layering/ parenting or instantiation

the instantiated object is declared as a TRANSFORM and you instantiate it as a GAMEOBJECT. Be sure to declare it as a GAMEOBJECT in your script.