Assigned enum value changes when a new type is added to the enum

I’d like to be able to add a new type in the mid of the enum, without changing all the assigned enum values in the editor.

To simplify I can show this example.
I start creating the enum and assigning it a value.

Then I decide to add a type inside the enum. But doing so it changes some of the assigned values in the editor.

The simplest solution would be just to add the new types always at the end of the list. But I’ve long enums, and if I keep doing that the code will start looking pretty messy.

Is there any way to make sure the assigned enums do not change also if I add a new type at the start or in the mid of the enum type list?

You just assign it a fixed value. By default, the first enum value is 0, and then incremented by 1.

Unity does not know you changed the enum class and it deserialize based on the value of the enum.

public enum TestEnum

According to your comment on @haruna9x answer I suggest you to check out the ISerializationCallbackReceiver interface.

With that you can “translate” fields into other fields before they get serialized (usually for making unserializable fields serializable). You could for example map your enums to string values in the OnBeforeSerialize function and then later back to the enums in the OnAfterDeserialize.

edit: that would require another ‘lookup’ variable though where you map the strings to the enum/int values and vice versa. Let me know if you need a sample