- Home /
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();
}
Yes... especially if you don't have an additional GUILayout outside of the if...
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. Ins$$anonymous$$d, 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.
Answer by Mike 3 · Dec 17, 2010 at 04:36 PM
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
Looks like I will be using GUI ins$$anonymous$$d, thanks!
Ah! Awesome, so it was just the GUILayout methods that cause this problem? Wow, thanks a ton!
I have a box and 2 labels (text and a texture) so far and it seems to work :)
Answer by Eric5h5 · Dec 17, 2010 at 07:26 PM
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 != "";
}
Your answer
Follow this Question
Related Questions
GUILayout ArgumentException when combining with GUI.Tooltip 1 Answer
Displaying a tooltip with GUILayout.Window 1 Answer
Text Alignment 2 Answers
Help with GUILayout and tooltip(C#) 1 Answer
For Loop GUILayout.Label Problems 1 Answer