- Home /
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
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.
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
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
This is really what I needed for a specific key . Thank You So $$anonymous$$UCH !
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;
}
Your answer
Follow this Question
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