Help with making changes to scene with editor

Hi everyone,

I’ve run into problems where changes to the scene performed by my custom inspectors are not preserved when I close Unity.

The first place where I have this issue is in a custom inspector for a script named “Constellation,” attached to objects with children with “Line” components. I tell the children to find a transform elsewhere in the scene (specifically, find a GameObject representing a star, based on its world position).

using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Constellation))]

public class ConstellationEditor : Editor
{
    public override void OnInspectorGUI()
    {
        Constellation constellation = target as Constellation;
        if (GUILayout.Button(“Set References”))
        {
            for (Line line in constellation.GetComponentsInChildren<Line>())
            {
                //  line will find a transform somewhere in the scene and reference it in a Serialized private field.
                line.FindReference();
            }
        }
    }
}

I also have a script called “StarField” which contains many GameObjects representing stars. Sometimes, I need to delete and load the stars, and I’ve attempted to implement those functions like so:

// setting up the custom inspector (sorry I got lazy this time)
public void LoadStars()
{
    if (File.Exists(Application.dataPath + "/starsData.csv"))
    {
        string dataString = File.ReadAllText(Application.dataPath + "/Resources/Stars - GameData.csv");
        foreach (string line in dataString.Split('\n'))
        {
            GameObject starObject = Instantiate(starPrefab, transform);
            // Do some other things to the star based on csv data.
        }
    }
}

public void DeleteAllStars()
{
    int childCount = transform.childCount;
    for (int i = 0; i < childCount; i++)
    {
        DestroyImmediate(transform.GetChild(childCount - i - 1).gameObject);
    }
}

As stated earlier, I’m unable to make lasting changes with these functions. Any help would be appreciated.

This is your magic sauce, right here:

@Kurt-Dekker The magic sauce doesn’t work for one of my problems. I’m trying to set a transform reference in another gameobject which is a prefab instance.

// This does not work.
Undo.RecordObject(player, "Set Reference");

// Nor does this.
PrefabUtility.RecordPrefabInstancePropertyModifications(player)

// Set a private Transform field of player to the transform of a Ball somewhere.
// player is a Prefab instance.
player.FindClosestBallReference();

“Doesn’t work” means that these changes don’t even persist through entering play mode, much less closing Unity.

In other news, I’ve solved my other problem with Undo.RegisterFullObjectHierarchyUndo. That was when I needed to delete all of my object’s children. So thanks for that.

Reading your response, I’m still slightly unclear if you’re telling me that Undo.RegisterFullObjectHierarchyUndo worked for the above or not? I thought that was the right one for anything transform-related… in any case, glad it’s working for you.

No, I had two problems to solve. Just ignore my last paragraph. Undo.RegisterFullObjectHierarchyUndo does not work to save changes made with player.FindClosestBallReference();, and neither does Undo.RecordObject nor RecordPrefabInstancePropertyModifications.

Undo.RegisterFullObjectHierarchyUndo takes the state of all a gameobject’s children, right? Even children of children?

I would defer to whatever the documentation says. I haven’t used it. If the documentation is unclear, you should indicate so at the bottom of the documentation page, because Unity does actually improve their documentation based on constructive feedback.