When is a String not a String???

This is driving me nuts.
I have a string that will not display in a label.

Here’s the full story…

In order to create tooltips that work with floating windows I’ve turn the tooltip into a window itself. This all works great except when I set the text on the label via a string that is stored in a class.

This is how I create the tooltip window

var myTooltip = ""; //This is a global variable outside OnGUI

function OnGUI() {
.
.
//various gui elements with tooltips
.
.
.
.
if (GUI.tooltip != "") myTooltip = GUI.tooltip;
else myTooltip = myClassFunction.getTooltip();

if (myTooltip != "") {
	Debug.Log(myTooltip); //this always echos correctly
	tooltipSize = mySkin.label.CalcSize(GUIContent(myTooltip) ); //this always calculates the correct size
	tooltipRect = Rect( mpos.x-20, mpos.y+10, tooltipSize.x+10, tooltipSize.y);
	GUI.Window(globals.tooltipID, tooltipRect, tooltipWindow, "", "BlankStyle");
}
}

This is the window code

function tooltipWindow (windowID : int) {
	Debug.Log(myTooltip); //this echos blank if I don't manually return a string from myClassFunction
	
	GUI.BringWindowToFront(windowID);
	
	GUI.Label( Rect( 0, 0, tooltipSize.x+10, tooltipSize.y), myTooltip, "Rollover");
	
}

As I say generally this works.
myTooltip is either set by the GUI.tooltip coming from the normal UI elements or it is set by the myClassFunction.getTooltip() funtion.

If I just return a string from that function like this

 function getTooltip() : String {
	return "Hello world";
}

Then everything works as expected and the “Hello world” tooltip appears over my floating GUI window and everything else.

However here’s where it starts getting weird.

If in that function I return a string that is itself created by another GUI.tooltip then the tooltip window displays a blank label. See attached images.

And the truly weird thing is I can do Debug.Log(myTooltip) and that will echo what I expect from the class tooltip always!

If however I do one Debug.Log in the first bit of code that creates the window and one in the actual tooltip window of the same thing, the first one is right and the second one is blank… how the heck does it reset myTooltip between the two? But that only happens when I don’t return “Hello world”… nuts.

Not only that but the line

tooltipSize = mySkin.label.CalcSize(GUIContent(myTooltip) );

always calculates correctly and so when the tooltip appears blank it is still the right size for the word coming from myTooltip. How is it CalcSize can see myTooltip but the actual label can’t.

191686--6790--$debuglog_137.jpg
191686--6791--$blank1_860.jpg
191686--6792--$helloworld_816.jpg

try changing this line:

var myTooltip = ""

to

static var myTooltip = ""

That will guarantee that you only ever have one instance of myTooltip

I did try that already, no change though.
What I’m finding is that GUI.tooltip sometimes returns nulls even when the mouse doesn’t move from an element. Whether this is something to do with being in a class or a window I don’t know. What I’ve ended up doing is only updating myTooltip when GUI.tooltip is not null. In order for that to work I have to make sure every element has a tooltip and only check for it when the mouse is inside the window rect. I then ignore and tooltips that are for elements I’m not interested in. It’s a hack but it works. If I get time I’ll strip it all down and send a bug scene off.

I would put a debug statement between setting the value and the second if-block to see if it’s being called and modified between the two function calls.

There is nothing between the 2 unfortunately. One is just before the line that calls the window and the second is the first line of the window code itself.

The tooltip is only recorded during repaint events, so it can be null when e.g. doing a layout event.

This is gonna be quite hard to make work with GUI.Window that only appears when there is a tooltip - since you don’t know until repaint time that you’ve got a tooltip, and the window needs to be created. Then again, instead of using a GUI.Window, how about just throwing a GUI.Label at the end of you OnGUI? That has way less setup/teardown work, and should just work

GUI labels don’t work for tooltips when using windowed content as they get clipped at the edge of the window. That’s why I switched to making the tooltip itself into a window so it could always be on top and fully visible.

I do have a work around now, as detailed above, but it would be less hacky if the tooltip value could be made persistent whilst layout event were happening.