Assigning Enum value in Inspector through custom script

Hi Guys,

I have a big enum list exposed in the inspector on one of my prefab.
Scrolling through the enum list in the inspector is quite a long and fastidious process (click on the small arrow at the bottom of the list and wait to reach the correct value).

So I decided to create a custom script that can assign the enum value to any selected object by entering a string. The tool works to assign the value (the enum list is updated correctly). But, unfortunately, when I play in the editor, the value resets to the last value that was set manually. It seems that using my custom script to assign the enum value doesn’t work.

So my question is the following:

  • Can we speed up the scrolling through the enum list (Does the wheel work for most of you? It doesn’t for me for the enum list).
    OR
  • How can I make sure that when I change an enum value through a custom script, it gets saved in the editor.

Thanks in advance.

What is this custom script you’re using to change it?

Is it an editor script?

Are you changing the value on the serializedObject or on the object itself?

If it’s the object itself, are you calling ‘Update’ on the serializedObject in question so that it too receives the data?

It is part of a custom tool used in an editor window.
So yes it’s an editor script.

I am changing the value on the prefab itself in the scene (not at run time - just to be clear)
I select it, press a button and it assigns the value.
I don’t call Update … (Actually… I am not sure what you are talking about :frowning: )

so all your things in scene actually have 2 objects that are part of it

  1. the actual Object in scene or in the folder hierarchy
  2. the serialized representation of that object

In your editor script if you say:

this.serializedObject

That’s a reference to the serialized object.

There are two methods on the serializedObject in question here.

SerializedObject.ApplyModifiedProperties

If you altered the SerializedObject, this applies them to the object in scene.

SerializedObject.Update

If you altered the in scene object, this applies those changes to the serializedObject.

In your code try saying:

this.serializedObject.Update();

After you’ve changed the enum value.

1 Like

The value resets because you are not marking as dirty (probably)

Hp,

Setting this in the editor script after I modified the enum list :

 EditorUtility.SetDirty(go);

doesn’t work.

Lord, I am going to try your version and let you know if it works.

Thanks for the feedback.

Lord,

Thanks for your help, it works. It was not that simple for me but I found out how to make it work.
I am posting part of the code I use to change my enum, in case someone wants to take a look.

        enumToEnter = GUILayout.TextArea(enumToEnter);
        string tmpEnumName;
        if(GUILayout.Button("Assign EnumTo Trap to Selection")){
            if(enumToEnter != ""){
                GameObject go = Selection.activeGameObject;
                SerializedObject so = new SerializedObject(go.GetComponent<LevelTransition>());
                if(go != null){
                    for(int x=0;x< Enum.GetValues(typeof(LevelTransition.LevelTransitionTo)).Length;x++){
                        tmpEnumName = Enum.GetName(typeof(LevelTransition.LevelTransitionTo), x);
                        if( tmpEnumName.Contains("TrapHole" + enumToEnter + "a")){

                            so.FindProperty("levelTransitionTo").enumValueIndex = x+1;
                            so.FindProperty("levelTransitionFrom").enumValueIndex = x;
                            so.ApplyModifiedProperties();
                          
                            Debug.LogWarning("Trap is updated!");
                            break;
                        }
                    }
                }
                else
                    Debug.LogError("Please select an object");
            }
            else
                Debug.LogError("There is no enumToEnter entered in the field.");
        }