Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 oxium · Jun 21, 2011 at 08:59 AM · serializationscriptableobjectinheritance

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

Hey guys !

I'm having a really weird (and annoying issue) at the moment..
Basically let's say I have something like this.. (sorry I don't like copy/pasting code but it should be easier to explain with this simple test code..)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization;
 [System.Serializable]
 public abstract class BaseClass : ScriptableObject
 {
     public enum eType
     {
         Type1 = 0, 
         Type2, 
         // etc..
     }
     
     public eType m_Type = eType.Type1;
     public bool m_Enabled = true;
     
     public virtual void Awake()
     {
         Debug.Log(m_Type + " Enabled = " + m_Enabled);
     }    
 }
 
 [System.Serializable]
 public class Type1Class : BaseClass
 {
     public GameObject     m_PrefabToInstantiate = null; // 3
     
     public enum eSecondaryType
     {
         AAA = 0,
         BBB,
         CCC,
     }
     
     [SerializeField] // 1
     public eSecondaryType m_SecondaryType = eSecondaryType.AAA;    
     
     [SerializeField] // 2
     public bool m_MySecondaryBool = true;
     
     public Transform m_Transform; // 4
     
     public Type1Class()
     {
         m_Type = BaseClass.eType.Type1;
     }    
     
     public override void Awake()
     {
         base.Awake();
         Debug.Log("From Type1Class: SecondaryType = " + m_SecondaryType + " MySecondaryBool = " + m_MySecondaryBool);
     }
     
 }
 


In the editor when I start my game, everything's working fine..
The Awake Functions both display what I'm expecting, so the serialisation is working here.. meaning Type and SecondaryType both display what I have in the UI (same for the 2 test booleans)

But strangely on iOS, the Awake function of Type1Class always return the default values.. where it's all fine for the BaseClass It's like if the serialisation was only done for the base class only..

Note that removing serialisation fields (markers 1 and 2) doesn't change anything at all and still can't get correct serialised values for m_SecondaryType and m_SecondaryBool

But it's not totally not working as the GameObject and Transform members are both correct... (markers 3 & 4) I guess I must be missing something obvious here related to basic type..
I will tomorrow try with other type like string or int...

Don't tell me to use MonoBehavior instead of ScriptableObject for my BaseClass it's just not possible for the design of the system I've implemented..

Anyway I have seriously no idea what is going on... so please if you have any idea don't hesitate as I'm really stuck on that right now... and it's pretty frustrating !

Comment
Add comment · Show 1
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 26, 2011 at 10:34 AM 0
Share

I'm pretty sure serialization doesn't support inheritance in Unity.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by angrypenguin · Feb 26, 2014 at 08:57 AM

As a heads up for anyone who may stumble upon this in future, abstract classes may be a potential issue.

I'm pretty sure I had sub-classes of an abstract parent serializing and deserializing earlier today without an issue. However, on a separate unrelated project (so other things could potentially be causing issues - testing has been very limited) (de)serializing to an asset outside of a scene was failing, and making the parent class non-abstract made it work.

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 mulova · Dec 17, 2011 at 04:58 PM

Unity CTO said that "Unity's serialization assumes that the type of an array does not change from element to element"

Comment
Add comment · Show 1 · 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 femi · Jan 26, 2012 at 12:59 PM 1
Share

he then went on to suggest "If you must use inheritance you have to inherit from ScriptableObject", which is what OP did, isn't it?

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

6 People are following this question.

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

Related Questions

ScriptableObject error with Inheritance 2 Answers

Scriptable Object List values are always null 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

Manually Save/Load Unity Serialization Data 0 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