• 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
0
Question by FakeMelon · Mar 20, 2018 at 06:58 PM · uiraycastclickblocking

How to make UI block clicks from triggering things behind it

Hey,

So I know this is an old question, but I can't seem to get the right answer anywhere. My problem is when I bring up my pause menu, for example, and then from that menu, I'll go to the settings menu (which in 3D you can see is positioned closer than the pause menu to the camera), I can click on an empty area around the settings menu and it will trigger a button that's on the pause menu behind it.

In short, I am looking for a way to make a UI block the clicks from going through it, triggering stuff behind it. Thank you.

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

7 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by RahulOfTheRamanEffect · Mar 26, 2018 at 03:26 PM

@FakeMelon

Someone has already given you the answer, but I'll try and expand on it a bit. This setup is what works for me:

Create a GameObject called "Blocker" between the "Settings Menu" GameObject and the "PauseMenu" GameObject, like so:

hierarchy
Note: The Blocker must be placed after the elements in the hierarchy that you wish to "block".

Now, as for the Blocker, set it up so that it covers the full canvas and add an Image component to it. Set the color as transparent and make sure to tick "Raycast Target", like so:

inspector
I've used this in a lot of applications to good effect. Unity draws the UI from bottom to top on the hierarchy, so by this hierarchy setup, Unity will draw the settings menu. Any touches outside the settings menu will be intercepted by the fullscreen "Blocker" that we just set up.

Enable the Blocker GameObject when you open the Settings menu and disable the Blocker when you close it.
(You could also achieve the same effect by just setting "Raycast Target" to true when the settings menu is shown and false when closed).

Hope that helps!


hierarchy.png (2.0 kB)
inspector.png (23.1 kB)
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 SimRuJ · Jun 11, 2021 at 09:13 AM 0
Share

Unfortunately this doesn't work anymore in Unity 2021.1.6f1. I want to block access to clickable GameObjects when a menu is up, so I added an additional panel that fills the whole screen as parent for the menu panels. The canvas is at the very bottom of the hierarchy and "Raycast Target" is enabled (by default) for all panels but I can still click through them.

avatar image awsapps · Sep 08, 2021 at 11:01 PM 0
Share

YEAH! this is what I was looking for, 100000 thanks! Note that, with this method, the OnClick functions won't be called, but you can continue reading the Input.AnyKeyDown in update components. SimRuj, works for me in 2021.1.11f1. Best regards

avatar image
2

Answer by dishant27 · Mar 20, 2018 at 07:03 PM

Create a full screen UI image between your front and back gameobjects. Set the transparency of image to 0.

Comment
Add comment · Show 12 · 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 FakeMelon · Mar 20, 2018 at 07:15 PM 0
Share

Tried putting the image in 2 ways. The first time it was the child of the settings menu's canvas but it completely disabled the menu's functionality for some reason. nothing worked anymore, no buttons, nothing. The second time I tried to put it under its own canvas, and then move the canvas with the image right behind the settings menu. It didn't work. clicks still triggered buttons behind the settings menu (and the image).

avatar image dishant27 FakeMelon · Mar 20, 2018 at 07:21 PM 0
Share

$$anonymous$$ake pop up content of pause, child of this image and make it active with pause button just like you were activating the pop up earlier.

avatar image FakeMelon dishant27 · Mar 20, 2018 at 07:28 PM 0
Share

I did what you said: https://fakemelon.tinytake.com/sf/$$anonymous$$jQ1$$anonymous$$zgwNF83NDEwNjc0

Still, clicks go through.

Show more comments
avatar image
0

Answer by upasnavig90 · Mar 22, 2018 at 12:17 PM

you can use a currentOpenScreen and on button click before performing action just check which screen is currently open.

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 FakeMelon · Mar 23, 2018 at 08:27 AM 0
Share

What's currentOpenScreen?

avatar image upasnavig90 FakeMelon · Mar 23, 2018 at 01:18 PM 0
Share

just take an enum of all the screens, and set its object to currently opened scene like this:public class Screen$$anonymous$$anager {

 public enum Screentype{
     Login,
     $$anonymous$$enu,
     GameOver,
     Stats,
     CategoryScreen,
     Achievements,
     Settings,
     Store,
     Profile,
     $$anonymous$$erboard,
 }
 public static Screentype currentScreenType;

} and use it like this:

     if (Screen$$anonymous$$anager.currentScreenType == Screen$$anonymous$$anager.Screentype.$$anonymous$$enu) {
                     //TODO:perform your action
                 }

avatar image
0

Answer by Khawar-Munir · Mar 22, 2018 at 12:53 PM

alt text@FakeMelon Make sure to check Raycast Target in the image component of your setting menu.alt text


raycast-target.png (9.3 kB)
raycast-target1.png (2.5 kB)
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 FakeMelon · Mar 23, 2018 at 08:28 AM 0
Share

It's checked, rays still go through it.

avatar image
0

Answer by Armend · May 21, 2020 at 12:34 AM

I know the question is old, but for those, who found this via google (like myself)

Follow the steps the other people suggested and add the components in the image. Important is the "blocks raycast" checkbox in the canvas group component.

And everybody should know this: https://unity3d.com/how-to/unity-ui-optimization-tips

[1]: /storage/temp/160018-canvsblocker.jpg


canvsblocker.jpg (48.0 kB)
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
  • 1
  • 2
  • ›

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

164 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 avatar image avatar image avatar image

Related Questions

Raycast against UI in world space 4 Answers

Canvas Graphics raycaster, prevent 2d and 3d physic raycast 0 Answers

Unity 5 new UI Button Colliders moved to the left in different resolutions 1 Answer

Create UI raycast like a mouse Click 2 Answers

Can't get results from GraphicRaycaster.Raycast 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