Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by jsmith11224 · Feb 25, 2018 at 03:17 AM · onmouseovergetmousebuttondown

OnMouseOver() with Input.GetMouseButtonDown(0) is being triggered for all instantiated objects from a prefab when I only click on one

Im instantiating from a prefab a series of objects (with canvas renderer, they're ui objects) that have the following script inside. I want a click to trigger only for the particular object i'm clicking on. Instead right now it triggers for all of them. It shows the specific variables of each individual item, but its triggering for all three at the same time. I'm stuck and don't know what else to tweak thanks for your help.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 using UnityEngine.UI;
 
 public class OnClick : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
 
 
         
     }
 
 
 
 
 
     void OnMouseOver(){
         
         if(Input.GetMouseButtonDown(0)){
 
             Debug.Log("clicked");
 
             /*
             GameObject clickedItem;
             clickedItem = gameObject;
             if (clickedItem == null) {
 
                 Debug.Log ("clickedItem is null");
 
             } else if (clickedItem != null) {
 
                 Debug.Log ("clickedItem is not null");
             }
 
 
 
 
 
             GameObject title = clickedItem.transform.GetChild (0).gameObject;
             if (title == null) {
                 Debug.Log ("title is null");
             } else if (title != null) {
                 Debug.Log ("title is not null");
             }
             //Destroy (title);
             Transform transform = title.transform;
             Text text1 = transform.GetComponent<Text>();
             if (text1 == null) {
                 Debug.Log ("text1 is null");
             } else if (text1 != null) {
                 Debug.Log ("text1 is not null");
             }
                 
             Debug.Log ("item text is: " + text1.text);
 
     */
 
 
         }
 
 
             
     }
 
     
     // Update is called once per frame
     void Update () {
 
         OnMouseOver ();
 
 
         
     }
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by UrielKane · Feb 25, 2018 at 03:31 AM

Hello. I'm not an expert on unity. That being said. As far as i can tell the issue is that you are calling the OnMouseOver from the update. So insted for detecting a single call for OnMouseOver you are getting several ones. Depending on what is exactly what are you trying to achieve i whould suggest using unityevents instead of OnMouseOver from script.

My button example

So if you are trying just to detect a hover event for like a few script coroutine call or a component acces like enabling or disabling something, you can just use unityevents and avoid those annoying and dirty things. In fact i have discovered unityevents just a few days ago and i was shock by the flexibility they give. Becouse before i was using them only on UI elements such as buttons or whatever. But now i know they can be used for scripts and you can for example use them on trigger events or that kind of stuff.

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.

Follow this Question

Answers Answers and Comments

128 People are following this question.

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

Related Questions

How to make spawned in game objects pause with mouse click. 1 Answer

How to make a user layer ignore raycasts of OnMouse* events? 1 Answer

!Onmouseover 1 Answer

How can I negatively addforce to an object based on mouse position? 0 Answers

OnMouseOver Polygon Collider2D "Layer order"? 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