I am working on creating a unidirectional flow framework in Unity.
Currently I am stuck on getting text updates to work the way I want. Here is how I would like it to work:
- I have a string stored on a script:
myString
- I can bind any number of other string's to it:
string1 = myString
,string2 = myString
etc - When I change
myString
, all other bound strings change as well
Here is my (of course not working) code:
public class Binder : MonoBehaviour
{
public Button button;
public string buttonText = "Hi";
void Start ()
{
buttonText = button.GetComponentInChildren<Text>().text;
}
// Update is called once per frame
void Update ()
{
buttonText = Time.time.ToString();
}
}
As is obvious, updating buttonText
does not update the button.GetComponentInChildren<Text>().text
(which stays at "Hi") because its just pointing buttonText
to another string.
If string was an object, I could mutate the object and it would update all references. That would be awesome.
My question is: do you know how I can mutate a string so all other pointers to the same string will of course show the same value? For example, say I store the string "Hello" and have two UnityButtons. I set those two UnityButtons.text to that "Hello" string. When I change "Hello" to "Bye" both of those buttons text also change to "Bye"?