• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Pigghaj · Jan 02, 2012 at 04:37 AM · instantiateonmouseup

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rabbitfang · Jan 02, 2012 at 07:26 AM 0
Share

The only thing I can think of is the script either not being enabled or not included in the prefab.

avatar image Jace · Jan 02, 2012 at 08:28 AM 1
Share

I agree with @rabbitfang it seems as if the script isn't included in the prefab. However you say "adding other things in the script work fine too". You mean these extra things work on the instaniated prefab? Try a Debug.Log in the Awake() function which should fire for each instance of the prefab. Other things that spring to $$anonymous$$d are, are you clicking the right object? Sounds silly but perhaps your instance is off camera and you're clicking something else. Just a thought

avatar image Grady · Jan 02, 2012 at 12:13 PM 0
Share

What is the actual code that you are using for the On$$anonymous$$ouseUp? Are you using tags to find the object? Or by the actual object's name??????

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by roamcel · Jan 02, 2012 at 01:24 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Pigghaj · Jan 02, 2012 at 02:30 PM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image roamcel · Jan 03, 2012 at 11:33 AM 0
Share

The only thing I can think of is a superimposition of colliders. If you instantiate your clickable object inside ANOTHER object which has a collider, it'll intercept the click.

avatar image Pigghaj · Jan 04, 2012 at 02:22 AM 0
Share

No, thats not it either. Very odd...

avatar image
1

Answer by ThePunisher · Jan 30, 2012 at 06:36 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if object intersects? 1 Answer

Instanitate Object with predefined script transforms 1 Answer

Random Instantiate onto Planet 4 Answers

Instantiate PreFab with C# Script 5 Answers

Array won't be filled by Instantiate 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges