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 /
avatar image
23
Question by tormentoarmagedoom · Sep 23, 2017 at 10:30 AM · uiraycastcanvasclicking

How to prevent clicking Gameobjects behind a Canvas/Panel?

Goo day all. (sry if bad english)

I have a scene with GameObjects that can be clicked. I also have an info panel at the bottom of the screen.

alt text

This panel have a hide/show function and it works well. The problem is when the panel is active, if the player click on the panel and ther is a gameobjects behind the panel, the gameobject is also clicked, and ofc, I dont want that. I need the gameobjjects to be "not clikable" if they are behind the panel.

How i prevent the click to "reach" the gameobject if there is a panel in front?

Maybe raycast is the solution? I've never used it... some another way to do it?

Thanks in advance!

panellunity.png (1.6 kB)
Comment
Add comment · Show 2
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 bolkay · Sep 23, 2017 at 11:17 AM 0
Share

I believe only buttons can be clicked at runtime. If this is correct, click on button and uncheck "interactable".

avatar image tormentoarmagedoom bolkay · Sep 24, 2017 at 11:01 AM 1
Share

Hello @bolkay

That is not true... everything with a collider can be clicked with On$$anonymous$$ouseDown() function

4 Replies

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

Answer by haruna9x · Sep 23, 2017 at 10:46 AM

 void Update()
     {
         // Check if the left mouse button was clicked
         if (Input.GetMouseButtonDown(0))
         {
             // Check if the mouse was clicked over a UI element
             if (EventSystem.current.IsPointerOverGameObject())
             {
                 Debug.Log("Clicked on the UI");
             }
         }
     }

IsPointerOverGameObject

Comment
Add comment · Show 5 · 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 tormentoarmagedoom · Sep 24, 2017 at 11:44 AM 3
Share

Thanks!

It works!, i had to do it negative of course, but it worked!

          if ( ! EventSystem.current.IsPointerOverGameObject())
          {
              DoSomethingBecauseIclikedTheObject()
          }
avatar image sampathcse16 · Nov 01, 2019 at 06:38 PM 0
Share

Basically you return or do nothing when the click happens on the panel.

          if (EventSystem.current.IsPointerOverGameObject())
          {
            //It means clicked on panel. So we do not consider this as click on game Object. Hence returning. 
              return;
          }
          else{
                //clicked directly on game object. 
          }
avatar image OrderOfOut · Apr 13, 2020 at 06:52 PM 0
Share

This doesn’t work, at least not in 2018.4. I have this exact script attached to a game object with a canvas backdrop, and I can still click through to game objects behind it, even with “Raycast Target” checked.

avatar image TriangularCube OrderOfOut · Jul 06, 2020 at 05:13 AM 0
Share

This is working for me in 2019.4. How are you using this particular piece of code?

avatar image d2clon · Feb 24, 2021 at 05:02 PM 0
Share

This the only thing that worked for me, Jason explains it here: https://www.youtube.com/watch?v=rATAnkClkWU&ab_channel=JasonWeimann

avatar image
31

Answer by frankslater · Sep 23, 2017 at 12:24 PM

I don't recommend checking for and analyzing mouse clicks in Game Objects' Update(). All objects that have the script are going to be checking and analyzing mouse clicks every single frame. That isn't a good practice and will quickly cause performance problems. There are different callbacks for input events that you can use.


Better to use IPointer Handlers like IPointerClickHandler, IPointerDownHandler, IPointerUpHandler and related callbacks.

Your clicks won't be registered through other objects.

If you use these, you only need to make sure that your panel is a Raycast Target (it is by default), and it's going to block out the Raycast from these callbacks. They also work better on touch screens than OnMouse callbacks.

(Clickable objects are always Raycast Targets. Others you might set just to block raycasts when over something.)

alt text


If you have something like this on your Game Objects, it won't be fired if a button, or panel, or anything else was clicked while your Game Object is behind:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class TouchForceExample : MonoBehaviour, IPointerClickHandler {
     public void OnPointerClick(PointerEventData data) {
         // This will only execute if the objects collider was the first hit by the click's raycast
         Debug.Log(gameObject.name + ": I was clicked!");
     }
 }



Another Example:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class TouchForceExample : MonoBehaviour, IPointerUpHandler, IPointerDownHandler {
     public void OnPointerDown (PointerEventData data) {
         ExamplePhysicsScript.instance.ForceIncrement ();
     }
 
     public void OnPointerUp (PointerEventData data) {
         ExamplePhysicsScript.instance.ForceSet ();
     }
 }

You can also use PointerEventData in interesting ways. For example to track the position of the pointer while it's down (so the user can drag something), or just to get a vector from pointer down to pointer up with PointerEventData.position.


capture.png (39.1 kB)
Comment
Add comment · Show 6 · 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 tormentoarmagedoom · Sep 24, 2017 at 11:16 AM 1
Share

Hello @frankslater !

$$anonymous$$aybe this could work, but with my code is not the perfect solution, is better to check if a UI was clicked. thx anyway :D

avatar image tormentoarmagedoom · Jan 23, 2019 at 05:12 PM 1
Share

I'm unsing this method in my new project :D

avatar image Jinxology · Sep 29, 2020 at 01:08 AM 0
Share

By far the better solution, thanks for this.

avatar image ddadkhah · Oct 11, 2020 at 12:30 PM 0
Share

when i use it for a cube, I don't get the click events

avatar image DallasP9124 · May 06, 2021 at 12:28 AM 0
Share

Not that this will only work with GameObject's within the canvas since they are the ones monitored by the EventSystem. IPointer* interfaces are in the EventSystems namespace so if you are trying to use this on other GO's, you wont get the event.

Also, the comment on raycasts being expensive is very misleading as it really depends on the depth of the game and its mechanics.

One way of improving is to have a reference to the RaycastHit and use that as the out variable, that way you save performance from GC per frame per object.

Show more comments
avatar image
5

Answer by Jon_Olive · Jan 26, 2019 at 05:37 PM

I realise this is an old thread - but I just found a simple solution: I made a transparent panel the size of the UI canvas. I arrange for everything I want to mask to be above it in the hierarchy and everything I don't to be below (a popup input dialogue for example). So when I call up the input dialogue I also enable the transparent panel. Job done!

Comment
Add comment · Show 1 · 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 Vorocity · Sep 21, 2019 at 09:00 PM 0
Share

Very simple, and worked nicely in my case. Thanks.

avatar image
1

Answer by omgbot · Apr 29, 2019 at 02:35 PM

For each object to hang such a code in the UPDATE is just a nightmare.

SUPER CODE :) Only MouseUP !!

     private void OnMouseUp()
     {
         ClickUI = false;
         if (EventSystem.current.IsPointerOverGameObject())
         {
             if (EventSystem.current.currentSelectedGameObject.name == "IcoDropDown")
             {
                 ClickUI = true;
             }
         }
         
         if(ClickUI){
            Debug.Log("Click UI button");
         }
     }
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 StaberCoder · Jun 01, 2020 at 08:40 AM 0
Share

hello this is probably a stupid question what do i define ClickUI as? @omgbot

avatar image bartendermew StaberCoder · Jun 08, 2020 at 10:36 AM 0
Share

private bool

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

161 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 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

Click on Canvas in a render texture 0 Answers

Can't interact with buttons in a UI Canvas in World Space Render Mode. 0 Answers

UI Buttons visible but not clickable when using two canvases. 3 Answers

How to get PointerEventDatas[]? 0 Answers

How to make a crosshair move with the player 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