I have a Unity Event that expects a string argument. Here is some code and usage example:
[Serializable]
public class StringUnityEvent : UnityEvent<string> { }
public StringUnityEvent onLineUpdate;
private void DummyFunction()
{
string text = "DummyText";
onLineUpdate.Invoke(text);
}
When I look in the inspector, I can select the text field of a UI object like this:
However, I’d like to set that dynamically through code. So far, the best I got is something like this:
void Start()
{
onLineUpdate.AddListener(SetTextField);
}
void SetTextField(string txt)
{
if (useBubbles)
{
dialogueBubble.GetComponentInChildren<Text>().text = txt;
}
else
{
dialogueContainer.GetComponentInChildren<Text>().text = txt;
}
}
Is there a better way to do this? Why can I select a field in the inspector, but I have to make the SetTextField method in order to create a listener through code? I’m quite new to Unity Events and can’t seem to formulate the search query correctly when trying to google this