Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Bovine · Oct 26, 2011 at 11:12 AM · inspectorscriptableobjectenumserializablenested

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oxium · Oct 31, 2011 at 03:37 AM 0
Share

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 ?

avatar image syclamoth · Oct 31, 2011 at 03:53 AM 0
Share

$$anonymous$$ake the NodeType enum serializable as well.

avatar image Bovine · Nov 02, 2011 at 02:24 PM 0
Share

@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...

avatar image dCalle · Aug 25, 2017 at 05:33 PM 0
Share

still an issue in unity 2017...

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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:

test result

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.


enumtestresult.png (20.1 kB)
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bovine · Oct 09, 2017 at 09:26 PM 0
Share

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.

avatar image Bunny83 Bovine · Oct 09, 2017 at 11:08 PM 0
Share

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.

avatar image NickMorhun90 · Jun 21, 2018 at 09:36 AM 0
Share

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.

avatar image Bunny83 NickMorhun90 · Jun 21, 2018 at 10:28 AM 0
Share

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

avatar image NickMorhun90 Bunny83 · Jul 09, 2018 at 08:49 AM 0
Share

$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges