Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by SomeRandomGuy · Jan 16, 2012 at 06:36 PM · guibuttononmousedown

Check if a button is NOT pressed?

Hi, I've got this grid of GUI buttons, and I basically want to execute some code whenever the player clicks anywhere BUT on one of these buttons, I've tried some things, but none worked so far, and I'm out of idea's :|

So the question is: Does anybody know of a way to check if no button was clicked? Some sort of variable perhaps? Something that would check if the mouse is over a button would work as well, but I can't seem to find anything. D:

If this is gonna be a hard to do thing I could just leave it out, but it would be a waste since it would mean some other functions I made wouldn't work properly anymore.

Anyways, Thanks in advance:D

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
2
Best Answer

Answer by Hybris · Jan 16, 2012 at 07:01 PM

In javascript

Try this:

 var clicked = false;
 
 function OnMouseDown(){

 clicked = true;

 }

 //in the Update now
 function Update(){
 
 //check if you clicked on the button
 if(!clicked){
    DoSomething();

  //going to make sure clicked doesnt stay true forever
 }else{
  clicked = false;
 }
 

Right now if its false, which it is by default, it does something, if you want to click anywhere exept the buttons(of course you want that, srry, but this way it is more easy to understand, I think...) then say :

 if(Input.GetMouseButton(0))
 if(!Clicked){
    DoSomething();

 }

Good Luck

PS: Im dutch too so: Veel geluk met je project.

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 SomeRandomGuy · Jan 16, 2012 at 07:44 PM 1
Share

Hmm I had thought of something similar to this, but the thing is, I have a grid of buttons, not just one button. So I can't just say on$$anonymous$$ouseDown as all those buttons have a function too.

But I just found a (kinda cheap) way of making it work, I basically just created a Rect over the whole grid size, and told it to do stuff when the mouse is not inside that Rect, which seems to work fine! :D

 if(Input.Get$$anonymous$$ouseButtonDown(0) && (open))
     {
         if (!rect.Contains(Input.mousePosition))
         {
             var ray : Ray = Camera.main.ScreenPointToRay(mousePos);
             curItem.transform.position = ray.GetPoint(2);
             curItem.GetComponent(Item).added = false;
             curItem.active = true;
             curItem = emptySlot;
         }
     }

the whole thing is for an inventory system and I wanted to be able to drop the selected item when I click anywhere but on a button.

But ofcourse simply using on$$anonymous$$ouseDown also applied the dropping to when I pressed a button, which made the drag 'n drop feature inside the inventory a bit useless as it would just drop the item when I would just want to place it somewhere else.

Anyways, thanks for the quick response, but I got it to work, haha!

PS: Bedankt! :D

avatar image
6

Answer by drudiverse · May 07, 2014 at 07:05 PM

this works and its easy:

     if ( !Input.GetKey("space" )){ print("notpressed"); }//prints if space not pressed
Comment
Add comment · Show 3 · 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 elegost · Sep 23, 2015 at 09:51 PM 0
Share

this is the easiest way

avatar image KarlKarl2000 · Jan 22, 2017 at 08:21 AM 0
Share

Sometimes the simplest answers are best!

avatar image TheYumma · Apr 20, 2017 at 01:20 PM 0
Share

This is really what I needed for a specific key . Thank You So $$anonymous$$UCH !

avatar image
1

Answer by ArgusSendMore · Oct 14, 2013 at 04:34 PM

For what it's worth, I ran into this problem when making an "exit" button on a mobile phone app. The above solution didn't work for me, as TouchPhase.Began seems to take precedence over everything.

My workaround was to make invisible buttons all around my primary button that detected if I clicked off of it. A blank GUI Style makes the buttons invisible.

 //This button will quit the game

         if (GUI.Button(Rect(Screen.width/2-120,Screen.height/2-60,240,121),exitTexture,customGuiStyle)){
             Application.Quit();
         }
         
 //These invisible buttons will unpause the game if any other area is clicked

         if (GUI.Button(Rect(0,0,Screen.width,Screen.height/2-60),blankTexture,customGuiStyle)){ //top
                 Time.timeScale = 1;
                 showExitButton = false;
         }
         
         if (GUI.Button(Rect(0,Screen.height/2+60,Screen.width,Screen.height/2-60),blankTexture,customGuiStyle)){//bottom
                 Time.timeScale = 1;
                 showExitButton = false;
         }
         
         if (GUI.Button(Rect(0,Screen.height/2-60,Screen.width/2-120,121),blankTexture,customGuiStyle)){//left
                 Time.timeScale = 1;
                 showExitButton = false;
         }
         
         if (GUI.Button(Rect(Screen.width/2+120,Screen.height/2-60,Screen.width/2-120,121),blankTexture,customGuiStyle)){//lef
                 Time.timeScale = 1;
                 showExitButton = false;
         }
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 guetta18 · Aug 05, 2018 at 02:24 PM 0
Share

excatly what i searched for!! tnx!!

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

11 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

Related Questions

Hide/show GUI Buion 1 Answer

OnMouseOver() OnMouseDown() button raycast help 1 Answer

Adding Force with a GUI button 5 Answers

GUI.Label positioning for many device resolutions 1 Answer

GUI, making main page lead into an instructions page 2 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