Change text in text component from editor script


I am currently working on a custom button script and I want to be able to change the text of the child text object in the editor without going to the text child. So I did a custom editor script that would allow me to do this.

This is my current code for doing this.

script.textChild.text = EditorGUILayout.TextField("Text", script.textChild.text);

(textChild is basically just a Text variable in the base script.) Now this works but you need to save the scene everytime you want to update the text. I would like it to update as you type. How can I do such a thing?

Thanks in advance

The text only changes if Unity notices that it has changed. In an editor script, Unity auto-detects that for whatever the Editor is targeting (your script variable) if you use EditorGUILayout, but changes to other objects are not noticed. You’ll have to set those dirty manually.

Luckily, that’s easy. Just check if the GUI was changed, and set the textChild dirty if that’s the case:

script.textChild.text = EditorGUILayout.TextField("Text", script.textChild.text);
if (GUI.changed) {

Just tested it: the text will update in the scene if you add the SetDirty line.