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 Seneral · May 25, 2015 at 02:16 PM · objectserializationscriptableobjectinheritanceabstract

Scriptable Object List values are always null

I need to save a list of base classes in a Scriptable Object, but currently all I'm seeing in the inspector is a list of null. That is the basic structure:

 public class Node_Canvas_Object : ScriptableObject 
 {
     public List<Node> nodes;
 }
 
 public abstract class Node : UnityEngine.Object
 {}
 
 [System.Serializable]
 public class CalcNode : Node 
 {}

Node is an abstract class serving as a base for alot more like CalcNode, though when I save the ScriptableObject Node_Canvas_Object the List of Nodes (containg CalcNodes and others) just contains a lot of null's.

What do I need to take care of to make this work?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by ThermalFusion · May 25, 2015 at 02:39 PM

Unity does not support serializing of inherited classes. You will need to use a class that unity serializes as a reference, like ScriptableObject. Take a look at this Answer for more information: http://answers.unity3d.com/questions/245604/does-unity-serialization-support-inheritence.html

Comment
Add comment · Show 4 · 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 Seneral · May 25, 2015 at 02:53 PM 0
Share

Thanks for the answer and the link! I'm going to try out making the parent class also a Scriptable Object. Do I have to save it seperately, too? Without I'm getting Type $$anonymous$$ismatch ins$$anonymous$$d of null

avatar image Seneral · May 25, 2015 at 03:24 PM 0
Share

I can save as an SubAsset now, but I'm having trouble identifying those when loading again. How would I go about that?

avatar image Seneral · May 25, 2015 at 03:56 PM 0
Share

Got it so far, here's the way I took:

 if (GUILayout.Button ("Load Canvas")) 
         {
             Node_Canvas_Object OldNodeCanvas = Object.Instantiate<Node_Canvas_Object> (nodeCanvas); // Backup
             nodeCanvas = null;
             Object[] objects = AssetDatabase.LoadAllAssetsAtPath ("Assets/Node_Editor/Canvas.asset");
             if (objects.Length == 0) 
             { // Revert
                 Debug.Log ("No objects found in the save file!");
                 nodeCanvas = OldNodeCanvas;
                 return;
             }
             List<Node> nodes = new List<Node> ();
 
             for (int cnt = 0; cnt < objects.Length; cnt++) 
             { // Important Part
                 if (objects [cnt].GetType () == typeof(Node_Canvas_Object)) 
                     nodeCanvas = objects [cnt] as Node_Canvas_Object;
                 else if (objects [cnt].GetType ().IsSubclassOf (typeof (Node)))
                     nodes.Add (objects [cnt] as Node);
             }
 
             if (nodeCanvas == null)
             { // Revert
                 Debug.Log ("No NodeCanvas found in the save file!");
                 nodeCanvas = OldNodeCanvas;
                 return;
             }
 
             nodeCanvas.nodes = nodes;
             Debug.Log ("Node Canvas Loaded!");
 
             Repaint ();
         }

With the code above this should make sense for everybody. Remember my base class, Node, now also inherits from ScriptableObject.

avatar image LaneFox · Dec 25, 2016 at 07:54 PM 0
Share

Just as an FYI, subclasses need to be in their own file or the serializer will have issues.

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

20 People are following this question.

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

Related Questions

Wrong serialization when using ScriptableObject + [System.Serializable] + inheritance 2 Answers

Weird Custom Editor bug 1 Answer

SubClass Reference becomes BaseClass after Serialization 0 Answers

Calling a derived class (Serialized in a Scriptable Object) returns the super class. 1 Answer

ISerializationCallbackReceiver not working on ScriptableObject 2 Answers


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