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();
    }

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

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 != "";
}