Strange Behaviour Using Enums C# Unity Version 3.0.0f5

Hello,

In my project I have a lot of Object that uses transparent Shaders.
Whenever I need, I use renderer.sharedMaterial.renderQueue to set the rendering order.

Sometimes, I need to change those values to fit new Objects. So, I created an Enum like this:

public enum QueueList
{
 Object1 = 3010,
 Object2 = 3020,
 Object3 - 3030,
}

And every object that uses this script has a public variable of type QueueList (called “queue”), so I can modify the renderQueue value inside the Inspector.
But whenever I change the original values from the enum, my “queue” variables doesnt reflect this changes. Instead, it’s like it is storing the INT value of the enum. For example:

If I have Prefab1 with QueueList queue = QueueList.Object1.
And then, inside my QueueList list script I change the value of Object1 to, lets say 3020, and the value of Object2 to 3010, the Prefab1 will have queue = QueueList.Object2.

Is this correct? The relationship shouldnt store the “String” value of the Enum, instead of the Int value?

An enumerated constant is in integer value fundamentally. What’s happening here is that the value is serialized as part of the component configuration; then you change the underlying values in the enum; and then the serialized value is read back in. The value (3010) hasn’t changed, but the symbolic name you’ve assigned for that value has. So on de-serialization, queue is read back with value 3010 and that value is matched to the symbolic constant, which has now changed to Object2.

There may be some way of overriding the serialization of your enum to avoid that with standard .NET serialization; unfortunately, that’s not what Unity uses so I doubt there’s a way around it without changing how you store and reference the data value :frowning:

This totally defeats the purpose of my enum. Sad =/

Thx for the explanation though laurie.

Is this something that should be fixed?

Serializing enums as ints means I can rename an enum element and still be able to deserialize old data, but if I remove or reorder an enum element than all my old data gets messed up. Serializing enums as strings would be preferable in my view – the “rename” case can then be handled by search and replacing in my .prefabs and scenes. But I guess it is what it is.