• 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 AlexMarchen · Jan 21, 2016 at 12:11 PM · uigameobjectbuttoncanvasonmousedown

OnMouseDown work through UI elements

There are 1 gameobject with onmousedown function and UI Button at my scene . When gameobject goes be$$anonymous$$nd button and i need to click t$$anonymous$$s button, function onmousedown of gameobject working too. How to avoid t$$anonymous$$s unwanted action ? I tried to attach collider to button, but function of gameobject still works.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by OnikurYH · Jan 22, 2016 at 02:00 AM

Hi, You can make a new script to check is the player hovering the UI Elements

For example (C#):

MyUIHoverListener.cs

Assume that the script is put into the Canvas game object

 using UnityEngine;
 using UnityEngine.EventSystems;
 using System.Collections;
 
 public class MyUIHoverListener : MonoBehaviour
 {
     public bool isUIOverride { get; private set; }
 
     void Update()
     {
         // It will turn true if hovering any UI Elements
         isUIOverride = EventSystem.current.IsPointerOverGameObject ();
     }
 }

MyGameObjectOnClick.cs

T$$anonymous$$s script is attach to your game object it will be$$anonymous$$nd the UI Elements

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class MyGameObjectOnClick : MonoBehaviour
 {
     private MyUIHoverListener uiListener;
 
     void Start()
     {
         uiListener = GameObject.Find ("Canvas").GetComponent<MyUIHoverListener> ();
     }
 
     void OnMouseDown ()
     {
         if (uiListener.isUIOverride)
         {
             Debug.Log ("Cancelled OnMouseDown! A UI element has override t$$anonymous$$s object!");
         }
         else
         {
             Debug.Log ("Object OnMouseDown");
         }
     }
 }

Hope t$$anonymous$$s can help you =)

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 AlexMarchen · Jan 22, 2016 at 09:29 AM 1
Share

Thank you so much!

avatar image Max-Boet · Sep 28, 2020 at 12:47 PM 0
Share

This answer saved my life, I couldn't understand why when a canvas is raycast blocking a collider, why does it still call OnMouseOver() event, but this fixes it, so..., cool!

avatar image
0

Answer by i2winners · Jan 10, 2021 at 01:20 AM

hey, I have added somet$$anonymous$$ng extra to your code , for if you open a UI after clicking the object (like a menu) and it clicks true the UI onto other objects that are under a button

`private void OnMouseDown()
    {
        if (!GameManager.instance.ObjectClickBlocker)
        {
            if (uiListener.IsUIOverride)
            {
                Debug.Log("Cancelled OnMouseDown! A UI element has override t$$anonymous$$s object!");
            }
            else
            {
                Debug.Log("Object OnMouseDown");
                ToggleBuildingMenu();
            }
        }
    }
    public void ToggleBuildingMenu()
    {
        BuildingMenu.SetActive(!BuildingMenu.activeSelf);
        GameManager.instance.ObjectClickBlocker = BuildingMenu.activeSelf; // sets same as menu, so if menu active blocker is active 
    }`

and n the GameManager add a public bool

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
0

Answer by d2clon · Feb 24, 2021 at 05:05 PM

T$$anonymous$$s is the only t$$anonymous$$ng that worked for me:

 if(!EventSystem.current.IsPointerOverGameObject())
 {
   // my code 
 }

Jason explains it here: https://www.youtube.com/watch?v=rATAnkClkWU&ab_channel=JasonWeimann

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

54 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

Related Questions

canvas child of another canvas issue 0 Answers

How do I root UI canvas GameObject 1 Answer

Empty space from canvas is rendering when I run my game when it shouldn’t 0 Answers

I can't use any UI elements in my Project 0 Answers

How can I organize menus as a tree hierarchy? 0 Answers


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