- Home /
GUI label dimensions bug
Probably I am overworked but that bug that I'm having is very wierd.
I draw stuff in rectangles:
void OnGUI ()
{
GUI.Label (headlineRect, "HEADLINERECT", headlineStyle);
GUI.Label (buttonRect, "BUTTONRECT", headlineStyle);
}
void Update ()
{
if (buttonRect.Contains (Input.mousePosition) && Input.GetMouseButtonDown (0)) {
doStuff()...
}
}
Seems legit to me. The Rectangles are created in Start(). Doesnt even matter because I can see the two labels in the game.
Now when I click the "BUTTONRECT" text in game nothing happens. But when I click the "HEADLINERECT" text, doStuff() happens.
I am using version 4.3.4 I tried to restart unity. When I change the buttonrect in update() to headlineRect it works like it should.
Sounds like you've switched your Rect dimensions in their declaration, or something to that effect.
Or you are placing them next to each other and miscalculating the dimensions somehow having one twice the height or width of the other and overlapping it.
Any reason why you are using GUI.Label ins$$anonymous$$d of GUI.Button?
Seems strange to write your own code when unity will carry out the functionality for you.
A reproducible example would be amazingly helpful; without one, I have to assume there's a problem in some of the code you're not showing.
I would normally agree about using GUI.Button, but I assume the usage of $$anonymous$$ouseButtonDown like this is intentional, since the behavior is different (GUI.Button activates when the button is released).
Answer by Eric5h5 · May 30, 2014 at 01:03 AM
You shouldn't use Input.mousePosition for OnGUI code, since it uses a different coordinate system. Or mix Update with OnGUI for that matter. Use Event.current.mousePosition (and EventType.MouseDown) put all the input code in OnGUI.
Answer by HotPhone · Jun 08, 2014 at 03:11 PM
I solved this with an own RectContains function. the y axis is in two different directions.
Your answer
Follow this Question
Related Questions
Touch screen coordinates is not same Rect class coordinates? 1 Answer
Input.mouse position on GUI Textures 2 Answers
Contains(Input.mousePosition) reacts to wrong rect 1 Answer
Best way to check where touched 1 Answer
Mouse moves over GUI 1 Answer