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
0
Question by Tset_Tsyung · Jul 19, 2017 at 03:37 PM · inspectorserializablecustom classserializefield

Custom abstract class (and inheritance) not allowing public array despite having System.Serializable?

Hey all,

I've searched similar posts, but must be missing something as I'm not getting the desired result.

Basically, playing around with basic AI to get understanding down. Have a base class as follows:

 [System.Serializable]
 public abstract class ActionBaseClass {
 
     public abstract void SetupAction(StateHandler stateHandler);
     public abstract bool AbleToPerformCheck();
     public abstract bool PerformAction();
 
 }

I then have a 'concrete' class that inherits from this:

 [System.Serializable]
 public class Action_MoveTo : ActionBaseClass {
 
     StateHandler ourHandler;
 
 ...

And finally a controller that SHOULD hold an array of the available actions to it - as follows:

 public class StateHandler : MonoBehaviour {
 
     [SerializeField] // I have tried it with this line missing as well.
     public ActionBaseClass currentAction;
     [SerializeField] // I have tried it with this line missing as well.
     public ActionBaseClass[] actions;
 
 ...

However when the scripts are compiled I am NOT seeing a public array or single variable in the inspector... alt text

As stated I have tried it with both "[SerializeField]" lines missing - however I'm still getting the same results. Any help would be gratefully appreciated.

Many thanks!

bugcap.png (4.8 kB)
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
1
Best Answer

Answer by Bunny83 · Jul 19, 2017 at 05:04 PM

Unity does not support inheritance when it comes to custom serializable classes. The serialization system does not serialize the actual instance but only the fields as "sub-fields". However it always uses the type of the variable and not the type of the instance that might be referenced. Since your variable type is "ActionBaseClass" it only serializes fields which are defined in that class.

If you need inheritance you have to use either MonoBehaviour or ScriptableObject derived classes.

For more information read the Script Serialization page carefully

Comment
Add comment · Show 3 · 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 Tset_Tsyung · Jul 20, 2017 at 01:25 PM 0
Share

Dear Bunny83,

$$anonymous$$any thanks for this answer. During my research I never came across that page - it was exactly what I needed. Again many thanks XD

avatar image Mjbgtaad56 · Feb 27, 2020 at 03:49 PM 0
Share

That sucks. I have a ScriptableObject for my level data, which requires inheritance. I'd prefer to not clutter my files with various scriptable objects for every single orientation of a tile. I know I should use the scene view for tile stuff, but for a puzzle game, I don't really like working that way.

avatar image Bunny83 Mjbgtaad56 · Feb 27, 2020 at 06:53 PM 0
Share

I'd prefer to not clutter my files with various scriptable objects for every single orientation of a tile

Why? It's generally recommended to place one class in one file and name the file accordingly. This provides a much better overview when browsing through the project files. It also helps when multiple people work on the same project since each script file is a seperate entity inside a C$$anonymous$$S and lowers the chance of conflicts.


$$anonymous$$ost Java applications even go a step further. Not only has every class its own file but the folder structure usualle maches the namespace hierarchy. While it sometimes seems like overcomplicating things it really helps organising your code in the long run and you will find the code you're looking for more quickly. If you have several subclasses of a certain type, just create a subfolder. There are many different ways how to organise a Unity project.


Finally note that while inheritance can be a useful tool in many cases, when it comes to serialization this is generally more an issue than a solution. $$anonymous$$aybe you should re-think your approach how you serialize your data. Note that the [ISerializationCallbackReceiver][1] interface can be used for various interesting workarounds. So you can transform your data manually into something Unity can serialize.


Though if you need help with your specific case you should post a seperate question. [1]: https://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.html

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

71 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Logic Problems using for loops 0 Answers

Why aren't my editor values saving when I press play? 3 Answers

Starting Play mode resets values for serialized class in the Inspector 0 Answers

Custom Inspector for an array of a serialized class 0 Answers

Using [SerializeField] vs public 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