Passing a Reference to a String in C#

  • Thread starter Thread starter Drakkith
  • Start date Start date
  • Tags Tags
    Reference String
Click For Summary
SUMMARY

The discussion centers on a C# programming issue in Unity where Class A updates a tooltip string every frame, but Class B fails to reflect these updates in its tooltip UI. The main problem arises from the misunderstanding of how reference types work in C#. Although strings are reference types, the method call from Class A to Class B does not maintain a live reference to the updated string. Instead, Class B only receives a copy of the string at the time of the method call. The solution involves ensuring that Class B has access to the updated tooltip text, potentially by passing a reference to an instance of Class B or updating the tooltip directly within Class B's Update method.

PREREQUISITES
  • Understanding of C# programming language
  • Familiarity with Unity game development environment
  • Knowledge of object-oriented programming concepts
  • Experience with Unity's MonoBehaviour lifecycle methods
NEXT STEPS
  • Learn about instance methods and static methods in C#
  • Research how to pass references between classes in C#
  • Explore Unity's documentation on MonoBehaviour and its lifecycle
  • Investigate how to implement event-driven updates in Unity for UI elements
USEFUL FOR

C# developers, Unity game developers, and anyone troubleshooting UI updates in game applications.

  • #31
pbuk said:
Yes that's right.

It's probably also worth mentioning that strings in C# are immutable: it is not possible to change their state. If you want to pass something to a function that modifies it then you can use the StringBuilder class.
Thanks, pbuk. I got around whatever limitation kept me from passing the reference to the original string around by creating my own class.
 
Technology news on Phys.org
  • #32
pbuk said:
strings in C# are immutable
Drakkith said:
limitation kept me from passing the reference to the original string

When you tried to change the "original string" somewhere else you didn't actually change it. you created a new string then assigned it to the "other" variable. The "original" happily hung out and was used for the tooltip.

BoB
 
  • Like
Likes   Reactions: harborsparrow and Drakkith
  • #33
rbelli1 said:
When you tried to change the "original string" somewhere else you didn't actually change it. you created a new string then assigned it to the "other" variable. The "original" happily hung out and was used for the tooltip.

BoB
Well that answers that. Thank you!
 
  • #34
Oh I'm an idiot. From my own link in post #26:

Because a string "modification" is actually a new string creation, you must use caution when you create references to strings. If you create a reference to a string, and then "modify" the original string, the reference will continue to point to the original object instead of the new object that was created when the string was modified.

The explanation was right in front of me as soon as I found that page and I just missed it completely.
 
  • #35
Drakkith said:
The explanation was right in front of me as soon as I found that page and I just missed it completely.
Sounds familiar... <wry smile emoji>
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K