Enable then disable all editor scripts on child game objects - to force saving of changed properties

I am facing an annoying issue with an editor script. I know editor scripts can be pretty complicated so hopefully, I can explain things in a clear way…

I have an editor script attached to a top-level game object which allows me to ray cast into the scene - select 2 game objects and draw a line between them.

I have managed to make it so the editor then saves the new line by calling my save logic on the OnDisable method. The issue I am facing is that the new line also has 2 child game objects that have a separate editor script related to them. I am finding that changes to these editors are not saved unless I select the child game objects before then saving the scene.

I have tried setting the child items to be the active game object for my child objects like this

Selection.activeGameObject = MyChildGameObject1;
Selection.activeGameObject = MyChildGameObject2;

but this then only triggers the child editor script for MyChildGameObject2
ideally, I would like to access the child editor scripts from parent editor script and call my save method.
Does anyone know how to do this? or suggest another method

Many thanks
Matt

This is a bit of a guess, but maybe after you make those changes you need to get a SerializedObject representation of the editor scripts and call serializedObject.Update(); or serializedObject.ApplyModifiedProperties(); on them before saving.

1 Like

Yeah, I was thinking along the exact same lines. This is infact what logic I have in the child editor scripts to save the changes to them. However, the problem is that when I am in the parent editor script I don’t have access to the child ones (or don’t know how to get access). If I subsequently select the child game objects in the scene then the child editor script (On enabled / disabled) events can be fired and the save logic can be called. But I am trying to avoid the need to select the child game objects manually.

You can create a new SerializedObject to access them.

var so = new SerializedObject(myUnityObject);

Hold a reference to a SerializedObject for each of your child objects and call the update and apply like you would normally.

Hi Karl,

In your code above would I create the SerializedObject using the child game object itself? or would it be a reference to the MonoBehaviour script attached to that child object (this script is then referenced by the child editor script)

Thanks
Matt

For the MonoBehaviours on the child game object

1 Like

Thank you, this worked perfectly!

1 Like