With the following method you only have to write the code for replacement once and can change the text in the Editor without digging into the source code again!
- Add a new text object:
- Create and attach a new script to the text object, something like “ReplaceValueBehavior”:
- In the Start() method of ReplaceValueBehavior.cs, set this code (and include UnityEngine.UI for Text):
var myValue = "world"; var txtObject = GetComponent<Text> (); txtObject.text = string.Format (txtObject.text, myValue);
Like this:
- Now set the text value of the text object to your output string and include a placeholder {0} for your variable:
- When the scene starts, the placeholder will be replaced by your value:
Done! 🙂