x


Why can't I get my tooltip to show only when there is a tooltip set?

If I use GUILayout, inside an if statement in OnGUI I get the following error:

ArgumentException: Getting control 0's position in a group with only 0 controls when doing Repaint

Any idea how to use GUILayout inside an if statement? Thanks!

    GUI.Box(new Rect(5, 35, 110, 75), 
            new GUIContent("Box", "this box has a tooltip"));

    if (GUI.tooltip != "") // <- This causes problems
    {
        var rect = new Rect(Screen.width / 2, Screen.height / 2, 300, 300);
        GUILayout.BeginArea(rect);
        GUILayout.Button("I am completely inside an Area");
        GUILayout.EndArea();
    }
more ▼

asked Dec 17 '10 at 02:53 PM

spaceshooter gravatar image

spaceshooter
249 49 55 69

Paste the if-statement....

Dec 17 '10 at 03:44 PM Statement ♦♦

Yes... especially if you don't have an additional GUILayout outside of the if...

Dec 17 '10 at 03:45 PM The_r0nin

OnGUI is called multiple times per frame and expects the program flow to be static for some of the different -events-. Basically it's safe to switch flow on if (GUILayout.Button()) and other GUI calls, but if you're checking if a key is pressed you might run into troubles. Instead, try doing that in Update and set state variables. Or read in depth about how the GUI system works in the manual. In my personal opinion (and off topic rant), unitys gui is a real horror show where tons can go wrong and it's often not immediately clear what is the cause.

Dec 17 '10 at 03:48 PM Statement ♦♦

It is just: if (GUI.tooltip != "")

Dec 17 '10 at 04:02 PM spaceshooter

Added the full OnGUI example to the main post.

Dec 17 '10 at 04:11 PM spaceshooter
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

GUILayout is a pain like that sometimes, and this case is one which won't be easily solvable.

What you could do is swap to GUI for just the tooltip - the code would be cleaner for your example anyway

if (GUI.tooltip != "")
{
    var rect = new Rect(Screen.width / 2, Screen.height / 2, 300, 300);
    GUI.Button(rect, GUI.tooltip);
}

Alternatively, you could use GUILayout to calculate the rects (by using GetRect), and then pushing them into the above function to actually draw the visible item

more ▼

answered Dec 17 '10 at 04:36 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

Looks like I will be using GUI instead, thanks!

Dec 17 '10 at 04:48 PM spaceshooter

Ah! Awesome, so it was just the GUILayout methods that cause this problem? Wow, thanks a ton!

Dec 17 '10 at 04:50 PM Statement ♦♦

I have a box and 2 labels (text and a texture) so far and it seems to work :)

Dec 17 '10 at 05:19 PM spaceshooter
(comments are locked)
10|3000 characters needed characters left

I've dealt with stuff like this before; the solution is to check for EventType.Repaint, and use a separate variable that tracks whether the tooltip is active or not, so the layout stuff can be properly computed in the right order:

private var tooltipActive = false;

function OnGUI () {
    GUI.Box(new Rect(5, 35, 110, 75), 
            new GUIContent("Box", "this box has a tooltip"));

    if (tooltipActive)
    {
        var rect = new Rect(Screen.width / 2, Screen.height / 2, 300, 300);
        GUILayout.BeginArea(rect);
        GUILayout.Button("I am completely inside an Area");
        GUILayout.EndArea();
    }

    if (Event.current.type == EventType.Repaint)
        tooltipActive = GUI.tooltip != "";
}
more ▼

answered Dec 17 '10 at 07:26 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3690
x137
x53

asked: Dec 17 '10 at 02:53 PM

Seen: 1781 times

Last Updated: Dec 17 '10 at 04:29 PM