Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 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 behind button and i need to click this button, function onmousedown of gameobject working too. How to avoid this 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

This script is attach to your game object it will behind 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 this object!");
         }
         else
         {
             Debug.Log ("Object OnMouseDown");
         }
     }
 }

Hope this 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 On$$anonymous$$ouseOver() event, but this fixes it, so..., cool!

avatar image
0

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

hey, I have added something 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 this 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

This is the only thing 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.

Follow this Question

Answers Answers and Comments

55 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

Related Questions

canvas child of another canvas issue 0 Answers

How do I root UI canvas GameObject 1 Answer

UI in World Space doesn't respond 1 Answer

UI buttons don't work with Canvas Render Mode: Screen Space - Camera. 2 Answers

Having issues creating a reset button (Unity 5.3.1) 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