- Home /
Why is Editor not persisting programmatic changes even when using EditorUtility.SetDirty()
Ok I have an editor script. It loops through all the doors in my level and assigns them unique ID's. Now, I know that changes made by script need to be regiestered so they are correctly saved so I am using both EditorUtility.SetDirty() and Undo.RegisterCompleteObjectUndo(). Originally I was just using the undo one but it wasn't saving so I added the SetDirty(). Now here's where things are breaking. When I run the command, the little * correctly appears at the top of the unity window indicating that the editor knows something has changed. Then I hit save, and the star goes away as expected. Then when I reload the scene, all of the changes are reverted!
What am I doing wrong? I thought registering the undo or calling set dirty was all I had to do to persist the change.
My Code:
#if UNITY_EDITOR
[CustomEditor(typeof(DoorManager))]
[CanEditMultipleObjects]
public class DoorAssignerEdtiro : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
GUILayout.BeginVertical();
if (GUILayout.Button("Assign IDs"))
{
DoorOpener[] Doors = GameObject.FindObjectsOfType<DoorOpener>();
for (int i = 0; i < Doors.Length; i++)
{
EditorUtility.SetDirty(Doors[i].gameObject);
Undo.RegisterCompleteObjectUndo(Doors[i].gameObject, "Updating Door ID");
Doors[i].DoorID = System.Guid.NewGuid().ToString();
Debug.Log(Doors[i].DoorID);
}
//Validate
for (int i = 0; i < Doors.Length; i++)
{
for(int j = 0; j < Doors.Length; j++)
{
if(i != j && Doors[i].DoorID == Doors[j].DoorID)
{
Debug.LogError("Doors " + Doors[i].name + " and " + Doors[j].name + " Have matching IDs!");
}
}
}
Debug.Log(Doors.Length + " Door Id's Assigned");
}
GUILayout.EndVertical();
}
}
#endif
Answer by CiberX15 · Sep 25, 2021 at 08:44 PM
Well... I feel stupid. Figured it out. I was doing:
EditorUtility.SetDirty(Doors[i].gameObject);
when I needed to be doing
EditorUtility.SetDirty(Doors[i]);
The parameter is a Unity.Object, not a GameObject like I thought it was. So I needed to supply it with the component that was actually being changed.
Your answer
Follow this Question
Related Questions
Change texture import settings by script 0 Answers
Custom editor script for folders? 1 Answer
Custom Editor Access to Scene Objects Variables (in the same way as UnityEvents) 1 Answer
Is there any way to create folders in the heirarchy to group objects? 1 Answer
How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers