- Home /
Knowing when a button is pressed, not released.
I'm having a major issue with Unity's GUI. I'm shooting a rocket whenever the player presses a GUI.Button. Here's the problem, the button is only active when the button is released, not pressed. I want to run some code when the button is pressed, not released. Any ideas?
Answer by Owen-Reynolds · Jan 13, 2013 at 03:30 AM
Unity buttons activate on a click and release because pretty all computer buttons work that way. To check for a click the "wrong" way, have to check the click and the area yourself:
You can check for the mouse clicked with Input.GetMouseButtonDown(0))
. Then if you look at Rect, it has an example in Contains: if (rect.Contains(Input.mousePosition))
. You'd check those in Update. Also note that mousePosition Y is pixels from the bottom, but a Rect for a GUI-button uses Y from top, so you may have to flip (`Screen.height - y`).
Answer by Wolfram · Jan 13, 2013 at 03:25 AM
Hm? AFAIK, GUI.Button already reacts to presses, not releases.
But take a look at http://docs.unity3d.com/Documentation/ScriptReference/GUI.RepeatButton.html
And please search Unity Answers before posting, similar questions have been asked before, and might have given you the hints you needed.
GUI-buttons react to a press -- they turn the active color -- but they don't return true (say they are pressed) until released.
Also, like "normal" buttons, both the click and release must occur over it, although you can move away and come back while holding it.
Ah, O$$anonymous$$. Never really used UnityGUI. Then he would need to construct some logic for himself, either using GUI.RepeatButton, or the method you described.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Create a Button Scrollview 0 Answers
float error with screen.width/height 0 Answers
Solved: Problem with shooting script 1 Answer