- Home /
Nested enum shows as int in Inspector
I have the following class structures:
public class NodeOwner : ScriptableObject
{
public Node Root = new Node();
}
[System.Serializable]
public class Node
{
public NodeType Type = NodeType.None;
public Node[] Children = null;
}
public enum NodeType
{
None,
Type1,
Type2
}
I have an editor script to create an instance of the NodeOwner. When I select the asset, the Root node appear fine in the inspector, with its Type set to None, but when I add children, their type defaults to 0 and I cannot choose node types via the inspector.
All help appreciated. I found this post but this doesn't seem to work for me:
http://answers.unity3d.com/questions/61073/enum-drop-down-menu-in-inspector-for-nested-arrays.html
Thanks Bovine
have you tried to move the enum inside your Node class ?
also your array of children should be of type NodeOwner ins$$anonymous$$d of Node, no ?
$$anonymous$$ake the NodeType enum serializable as well.
@syclamoth making the NodeType serializable didn't have any effect, but it was working at the top level.
@Oxium - sadly I have too much code in place elsewhere to try this, but in fairness I have moved the enum so often I've probably tried it.
The issue is somewhat moot as I hav a custom editor anyway now...
Answer by hexagonius · Aug 26, 2017 at 03:54 AM
I had a similar issue related to classes inheriting from another with an enum, showing that as integers. This is worth a bug report I'd say.
Answer by Bunny83 · Aug 26, 2017 at 04:30 AM
I just thrown together a test case and can't reproduce this problem under any circumstances.
My test cases look like this:
public enum ETestEnum
{
One, Two, Three
}
[System.Serializable]
public class DeepNested
{
public string name;
public ETestEnum enumVal;
}
[System.Serializable]
public class NestedClass
{
public string name;
public ETestEnum enumVal;
public DeepNested child;
}
[System.Serializable]
public class DerivedClass : NestedClass
{
public ETestEnum secondEnumVal;
}
public class TestScript : MonoBehaviour
{
public DerivedClass[] classInst;
}
public class TestScriptable : ScriptableObject
{
public DerivedClass[] childs;
}
I've actually tried every data class. I was extending the example step by step. I have even deep nested classes as well as a derived class and the enum always shows up as a drop down field:
Some further notes:
Keep in mind that custom serialized classes do not support polymorphism. You can use a derived class as long as its base classes and itself is serializable, but you can't use a base class variable and store a derived class in that variable. Unity's serialization system treats such classes like structs. They are serialized based on the variable type.
Custom serialized classes must not create any kind of circular reference. In the OP code the Node class has an array of Nodes. This does not work. The serialization system will throw an error in this case.
I strongly recommend to read the documentation on script serialization carefully. Make sure you really understand everything that is stated there. If something is not clear, feel free to ask a question. Please DO NOT post a comment or even an answer to this question.
If someone has an actual test case which reproduces this problem, please leave a comment. It would be great if you could tell us your operating system and unity version. My tests were carried out on Win10(64bit) and Unity 5.6.1f1 Personal. Maybe you can pack the test case into a zip file and upload it for others to test.
This may well be fixed now. If I get to look at Unity again in any anger, I may see if this is so. I note that my example is 'hosting' the class within a ScriptableObject, but I imagine if this works for a $$anonymous$$onoBehabiour, it ought to work for the former.
I've posted this answer mainly because of the necro comment of dCalle who said that he has that problem even in Unity 2017. However i can't reproduce this problem.
I can confirm that this still occurs even in 2018.1. But ONLY when you attempt to serialize a tree. Why did you create another "test case" without a tree? The tree is necessary to see the issue. I have now read that trees are not officially supported. But there is NO error, just warnings about 7 levels of nesting. And it's really strange why they refuse to support this because the references are not strictly "circular", they reference an array/list of children which will be empty for leaf nodes.
Nested serialization is not supported. The serializer will just refuse to properly serialize your construct as it has to error out during serialization. $$anonymous$$eep in $$anonymous$$d that custom serializable classes are treated like structs by the serialization system. Your comment just tells me that you did not read the documentaion i've linked above. Serialized references are only possible with references to other assets / UnityEngine.Object derived classes. The only classes you can use as a baseclass for your custom classes are ScriptableObject and $$anonymous$$onoBehaviour. Those are ot serialized inline but seperately.
They changed the old serialization depth error into a warning since people complained getting spammed with the error messages. Though it would have been better to keep the error spam$$anonymous$$g to make it clear: what you've done here does not work / is not supported.
edit
I just found the old forum post which was posted at the time they changed the error into a warning
$$anonymous$$y previous comment clearly states that
I have now read that trees are not officially supported
so I did read the docs... The only point of my comment was to say that I strongly believe that they can support trees if they had a reason to do so.
Your answer
Follow this Question
Related Questions
Enum drop down menu in inspector for nested arrays 2 Answers
Scriptable Object only useable from code, not via inspector 1 Answer
Make the same serialized class as a ScriptableObject? 1 Answer
How can I refactor an enum so that it is still available in the inspector? 1 Answer
Inspector - Show/hide property within custom class using enum 1 Answer