- Home /
enum in inspector?
I used this http://wiki.unity3d.com/index.php/ExposePropertiesInInspector_Generic for a bool and it worked fine, but when I used it on an enum it gave weird result. Code:
public enum TileDirection
{
Right,
Left,
Top,
Bottom
}
[ExposeProperty]
public TileDirection Direction
{
set
{
direction = value;
Debug.Log(direction);
}
get { return direction; }
}
[HideInInspector, SerializeField]
private TileDirection direction;
when I changed from Bottom to Right I got this:
Am I doing something wrong?
after some testing, I found out that the code that I got from the link got some problems with enum.
In case you don't know, you can change the inspector to Debug $$anonymous$$ode, which will display your private variables.
https://docs.unity3d.com/$$anonymous$$anual/InspectorOptions.html
Answer by Bunny83 · Sep 30, 2016 at 12:25 AM
The problem is most likely that the condition in this line:
if (oldValue != newValue)
will always evaluate to "true" for enum values as the "Enum" type is a class type. So the value will be boxed and comparing boxed values will compare the references and not the actual values.
To fix this issue you would have to change the way how you compare the values inside the "Expose" method:
if (oldValue != null && oldValue.Equals(newValue))
field.SetValue(newValue);
Answer by elenzil · Sep 29, 2016 at 05:48 PM
tack on ".ToString()".
eg:
Debug.Log(direction.ToString());
Your answer
![](https://koobas.hobune.stream/wayback/20220612093706im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
DropDownList with string array in Editor Inspector 5 Answers
[Inspector] Populate Enum Options with List of Files? 1 Answer
Is "enum x : UInt32" cast to "enum x : int" when serializing? 1 Answer
Showing properties from a base class in inspector? 0 Answers
What are "template custom keys" in the Player settings? 0 Answers