• 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 Famattsson · Aug 10, 2019 at 11:39 PM · editorinspectorserializationstats

Populating list of custom class through inspector

HI!

I have a list of my abstract custom class "Stat" on my items in a game i'm making. I want to be able to populate t$$anonymous$$s list through the inspector so that i can make multiple, different items easily. The problem is that i can't create instances of the "Stat" class through the inspector.

"Stat" class with sublasses:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 abstract public class Stat : MonoBehaviour {
     public int value;
 
     public Stat (int value) {
         t$$anonymous$$s.value = value;
     }
    
     abstract public string Print() ;
 }
 
 
 [System.Serializable]
 public class FireRateStat : Stat {
 
     public FireRateStat(int value) : base(value) {
         t$$anonymous$$s.value = value;
     }
 
     public override string Print() {
         return "Fire rate: " + value + "/n";
     }
 }
 
 [System.Serializable]
 public class DamageStat : Stat {
     public DamageStat(int value) : base(value) {
         t$$anonymous$$s.value = value;
     }
 
     public override string Print() {
         return "Damage: " + value + "/n";
     }
 }
 
 [System.Serializable]
 public class HealthStat : Stat {
 
     public HealthStat(int value) : base(value) {
         t$$anonymous$$s.value = value;
     }
 
     public override string Print() {
         return "Health: " + value + "/n";
     }
 }
 
 [System.Serializable]
 public class HealthRegStat : Stat {
 
     public HealthRegStat(int value) : base(value) {
         t$$anonymous$$s.value = value;
     }
 
     public override string Print() {
         return "Health regen: " + value + "/n";
     }
 }
 
 [System.Serializable]
 public class S$$anonymous$$eldStat : Stat {
 
     public S$$anonymous$$eldStat(int value) : base(value) {
         t$$anonymous$$s.value = value;
     }
 
     public override string Print() {
         return "S$$anonymous$$eld: " + value + "\n";
     }
 }
 
 [System.Serializable]
 public class S$$anonymous$$eldRegStat : Stat {
 
     public S$$anonymous$$eldRegStat(int value) : base(value) {
         t$$anonymous$$s.value = value;
     }
 
     public override string Print() {
         return "S$$anonymous$$eld Regen: " + value + "/n";
     }
 }
 
 [System.Serializable]
 public class MoveSpeedStat : Stat {
 
     public MoveSpeedStat(int value) : base(value) {
         t$$anonymous$$s.value = value;
     }
 
     public override string Print() {
         return "Move Speed: " + value + "/n";
     }
 }

Item class:

 using System.Collections;
 using System.Collections.Generic;
 using System.Reflection;
 using UnityEngine;
 
 public enum Category
 {
     None,
     Engine,
     S$$anonymous$$eld,
     Weapon,
     Chassis,
 }
 
 public class S$$anonymous$$pPart : MonoBehaviour {
 
     PlayerStats player;
 
     [TextArea]
     public string description;
     [TextArea]
     public string partName;
 
     public int buyValue;
     public int sellValue;
     public List<Stat> stats;
 
     [HideInInspector]
     public ItemImage itemImage;
     [HideInInspector]
     public string statsText = "";
 
     public Category category;
 
 
     public void UnassignSelf(bool removeItem = true) {
         if(itemImage != null)
         {
             itemImage.UnassignItem(removeItem);
         }
     }
 
     private void CreateStatsText () {
         foreach (Stat stat in stats) {
             statsText += stat.Print();
         }
     }
 
     public void UnapplyStats() {
         Debug.Log("Not implemented yet");
     }
 
     public List<Stat> GetStats() {
         return stats;
     }
 
     public void Start () {
 
         CreateStatsText();
     }
 }
 

Current inspector: alt text

5692f265a75616be03a6efd8bd158f80.png (13.6 kB)
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 KpyaccaH · Jul 27, 2022 at 01:21 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Aug 11, 2019 at 01:21 AM

What you want to do doesn't work. At the moment your "Stat" class is a MonoBehaviour derived class and not a custom serializable class. MonoBehaviours do support inheritance but are components and have to be attached as component to a gameobject.


Actual custom serializable classes must not be derived from MonoBehaviour. However they do not support inheritance at all. Custom serializable classes are serialized inline based on the variable type. They are not serialized as seperate objects. Unity just serializes the fields as sub fields of the hosting monobehaviour class. No type information is stored for custom serializable classes.


MonoBehaciour derived classes do support inheritance but they are seperate object instances and will not be serialized or displayed inline in the inspector. They need to be attached as seperate components to a gameobject. When you have a field to a "stat" class it will only be a serializable reference just like you see in your screenshot. So you can drag in a reference to an attached component.


Your exact usecase is not clear. How you want to manage or use those stat classes. Note that if you want to use actual components you can not define a custom constructor as the objects are created by dragging the script of such a component onto a gameobject in the inspector or by using gameObject.AddComponent<YourStatType>(). Keep in mind that MonoBehaviour derived types need to be located in their own script file and the file name need to match the class name contained in the file.

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Displaying System.objects not serialized by Unity in a Custom Editor 0 Answers

Showing properties from a base class in inspector? 0 Answers

Unable to draw propertyfield in inspector for some classes, findProperty returns null. 0 Answers

Dictionary in inspector 6 Answers

How should I serialize data that is also editable in the Inspector? 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