- #1
- 23,094
- 7,504
- TL;DR Summary
- Trying to update the text for a tooltip each frame.
I'm using C# to program in Unity and I'm having some trouble with getting a tooltip box to update every frame.
Simplified, Class A (and several other classes and instances of classes) has a string that stores the tooltip text and updates it every frame.
Class B contains the logic to generate the tooltip box and everything inside it.
Class A calls a method in Class B with a GenerateTooltip method linked to an OnMouseOver event.
The issue is that even though the tooltip text is being updated every frame, Class B doesn't appear to have access to this updated information and only shows the original text passed through the function. Simplified code:
However, this doesn't seem to work. My understanding was that strings are reference types and thus Class B would have access to the updated tooltipText variable from Class A each frame. But apparently I am incorrect and reference types don't work like this. Can someone explain why this doesn't work and what I could do to fix it?
Simplified, Class A (and several other classes and instances of classes) has a string that stores the tooltip text and updates it every frame.
Class B contains the logic to generate the tooltip box and everything inside it.
Class A calls a method in Class B with a GenerateTooltip method linked to an OnMouseOver event.
The issue is that even though the tooltip text is being updated every frame, Class B doesn't appear to have access to this updated information and only shows the original text passed through the function. Simplified code:
C#:
public Class ClassA{
string tooltipText;
void Update(){ //Called every frame by Unity.
UpdateTooltipText(); //Updates tooltipText variable with various things that can change each frame.
}
void GenerateTooltip(){ //Called with a mouseover event.
ClassB.CreateTooltip(tooltipText) //Passes tooltip text to Class B.
}
}
public Class ClassB{
public CreateTooltip(string passedTooltipText){
tooltipBox.text = passedTooltipText; //Updates the tooltip UI label with the correct text to display.
//Logic to generate tooltip of the right size and position.
}
void Update(){ //Called each frame.
if (tooltipVisible){
tooltipBox.text = tooltipText; //Updates the tooltip UI box each frame, but tooltipText
//is not updating based on what Class A is doing to its tooltipText variable.
}
}
}
However, this doesn't seem to work. My understanding was that strings are reference types and thus Class B would have access to the updated tooltipText variable from Class A each frame. But apparently I am incorrect and reference types don't work like this. Can someone explain why this doesn't work and what I could do to fix it?