- Home /
Custom editor enum resets on play
Hi
I am working on getting a custom enum in my editor so I can choose a child and then when the game starts, parent a object to the object set in the enum. But every time I start the game it gets reset. I don't know what causes this to happen.
Here's my code:
public List<GameObject> children = new List<GameObject>();
int oldIndex = 0;
void OnEnable()
{
targetScript = (FPP_Flashlight)target;
foreach (Transform t in targetScript.transform)
{
children.Add(t.gameObject);
}
for (int i = 0; i < children.Count; i++)
{
foreach (Transform t in children[i].transform)
{
children.Add(t.gameObject);
}
}
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUI.BeginChangeCheck();
string[] names = new string[children.Count];
int[] values = new int[names.Length];
for (int i = 0; i < names.Length; i++)
{
names[i] = children[i] != null ? children[i].name : "";
values[i] = i;
}
var index = EditorGUILayout.IntPopup("Parent", targetScript.parentIndex, names, values);
if (EditorGUI.EndChangeCheck())
{
targetScript.parentIndex = index;
Debug.Log("Set parent index to " + index);
targetScript.parent = children[index];
if (oldIndex != index)
{
oldIndex = index;
}
}
serializedObject.ApplyModifiedProperties();
}
}
Any help is appreciated!
You shouldn't mix using "target" and "serializedObject". I'm not sure if that might be the problem but they most likely will work against each other. When you call "Apply$$anonymous$$odifiedProperties" the serialized object will apply the changed values inside the serialized object to the actual asset. Since you capture the values at the beginning by using serializedObject.Update();
and just apply them back at the end doesn't make much sense. You don't use the serializedObject at all so just remove them. Using "target" also makes your editor a single-object-editor. So no multiobject editing is possible.
Also "parentIndex" seems to be an integer variable and not an enum. The title is a bit confusing / misleading. You just display a custom dropdown field to select a child index as it looks like.
As long as parentIndex is actually a serialized value i don't see anything else wrong here.
If you want to use the serialized object ins$$anonymous$$d of "target" you should post your declaration of your parentIndex field in your FPP_Flashlight class.
So should I just do something like "targetScript.variable = EditorGUI..."?
Uhm, yes ^^. I'm not sure if it's still necessary to use EditorUtility.SetDirty but i guess it is. "target" is actually the old way how the inspector was handling things. Since Unity has multi selection support you might want to use serializedObject ins$$anonymous$$d. See Statements answer for more details.
That's why i asked for the actual declaration of your variable. You usually use FindProperty to get a SerializedProperty of your variable. It allows to change all instances at once when you have selected multiple instances of your object. "target" can only work on one object at a time. See CanEdit$$anonymous$$ultipleObjects and the example on the Editor docs page. They have two examples. One that uses serializedObject and one that uses "target".
Answer by Statement · Oct 31, 2015 at 07:33 PM
targetScript.parentIndex = index;
I think you need to set the parentIndex through a SerializedProperty for the change to stick. It's been a while since I messed around with those so I may have forgotton/made assumptions on usage.
SerializedObject
SerializedObject.FindProperty
SerializedProperty
Thanks for the help but I decided to go with @Bunny83 's solution and scrap SerializedObject. Sorry for the inconvenience.
Hey no worries. Do what you feel most comfortable with. Although I think Bunny hints that it could be desirable to use SerializedObject for multiediting support. It'll make your editor to mainstream to the Unity user experience where you can select multiple objects and perform the same modification on all of them. Though if you don't want to support/learn that, nor support undo (I may be wrong here, but iirc you'd have to use SerializedObject or record the state of the object with the Undo class), I guess you can skip it.
Your answer
Follow this Question
Related Questions
How can I display another enum? 1 Answer
Custom PropertyDrawer to support the Flag is not change value. 0 Answers
Custom Editors : EnumFields, Proper Use Of 0 Answers
How to create a new SerializedProperty? 0 Answers
Custom editors, enums and arrays 0 Answers