Variable won't change, any suggestions?

Hi!

I am using a custom tooltip function (credits to AngryAnt). But I have a problem when trying to make it possible to have multiple tooltips. In the post as far as I understand, “tooltip” is the string to be replaced by the tooltip text, so I changed it to:

void StringToolTip (Rect rect, string toReplace, string lastToolTip)
	{
		if (rect.Contains (Event.current.mousePosition))
		{
			toReplace = lastToolTip;
		}
	}

void TooltipLastLayout(string toReplace, string lastToolTip)
{
	StringToolTip (GUILayoutUtility.GetLastRect (), toReplace, lastToolTip);
}

And call it like this in OnGUI:

TooltipLastLayout (stringToDisplay, "This is a tooltip");

Where “stringToDisplay” is used by the label where the tooltip are to be displayed.

The problem is that “toReplace” won’t change to “lastToolTip”. I think it has something to do with the fact that you can’t assign a value that is “layered” like “toReplace” (?) Does anyone know why this is happening?

toReplace is a local variable within the StringToolTip function. You can assign in whatever you want within that function, but that toReplace is a completely different memory location that the toReplace variable in your ToolTipLastLayout function, even though you have named them the same thing.

What I would suggest is that you change the signature of StringToolTip to this:

string StringToolTip (…) {…}

and then you can return the updated value. So your code could be something like:

string StringToolTip (Rect rect, string toReplace, string lastToolTip)
    {
       if (rect.Contains (Event.current.mousePosition))
       {
         return lastToolTip;
       }

       return toReplace;
    }
 
string TooltipLastLayout(string toReplace, string lastToolTip)
{
    toReplace = StringToolTip (GUILayoutUtility.GetLastRect (), toReplace, lastToolTip);
    return toReplace;
}

You need to put a ref in front of the string parameters that are to change - then you can change the value that is being passed in. Or you could return strings from both of the functions.