- Home /
Changing Enum var order messes up Enums saved in Scriptable Objects.
If I save out an Enum to a Scriptable Object, if I then change the order of the Enum items (by say adding one in the middle), all the Scriptable Objects saved enums will mess up. They will change to some other enum. Is this because it's stored as the underlying int and when that int becomes a different enum it becomes that? Thanks. I guess the solution would to be just save them as a string from the enum.
Answer by Bunny83 · Jan 11, 2020 at 04:42 AM
You haven't provided an example how you define your enum. However I guess you just use the "short form". Something like that
public enum MyEnum
{
ItemOne,
ItemTwo,
ItemThree
}
This is actually equivalent to the full form
public enum MyEnum : int
{
ItemOne = 0,
ItemTwo = 1,
ItemThree = 2
}
Note that whenever you omit the value of an item, it's simply continued from the last defined one. If you do not define any value it just starts at value 0. For example
public enum MyEnum
{
ItemOne = 5, // has value 5
ItemTwo, // has value 6
ItemThree, // has value 7
ItemFour = 42, // has value 42
ItemFive, // has value 43
ItemSix = 7, // also has value 7 like ItemThree
ItemSeven // has value 8
}
Yes, I am doing it the short form way. Interesting, so if I manually define them, then I shouldn't have any problems. What happens when two enums have the same int value like you mentioned? Thank you.
Answer by xploreygames · Jul 29, 2020 at 04:30 PM
I needed to add a new value to the top of my enums so using the method Bunny83 mentioned, I did Item0 = -1, (so it didn't change all the values behind it which started at 0).