Search Unity

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

Discussion in 'Editor & General Support' started by matt33315, Oct 3, 2019.

  1. matt33315

    matt33315

    Joined:
    Mar 28, 2014
    Posts:
    94
    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
    Code (CSharp):
    1.  
    2. Selection.activeGameObject = MyChildGameObject1;
    3. Selection.activeGameObject = MyChildGameObject2;
    4.  
    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
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    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.
     
    karl_jones likes this.
  3. matt33315

    matt33315

    Joined:
    Mar 28, 2014
    Posts:
    94
    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.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    You can create a new SerializedObject to access them.
    Code (csharp):
    1. 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.
     
  5. matt33315

    matt33315

    Joined:
    Mar 28, 2014
    Posts:
    94
    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
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    For the MonoBehaviours on the child game object
     
    matt33315 likes this.
  7. matt33315

    matt33315

    Joined:
    Mar 28, 2014
    Posts:
    94
    Thank you, this worked perfectly!
     
    karl_jones likes this.