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 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 this 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) {
         this.value = value;
     }
    
     abstract public string Print() ;
 }
 
 
 [System.Serializable]
 public class FireRateStat : Stat {
 
     public FireRateStat(int value) : base(value) {
         this.value = value;
     }
 
     public override string Print() {
         return "Fire rate: " + value + "/n";
     }
 }
 
 [System.Serializable]
 public class DamageStat : Stat {
     public DamageStat(int value) : base(value) {
         this.value = value;
     }
 
     public override string Print() {
         return "Damage: " + value + "/n";
     }
 }
 
 [System.Serializable]
 public class HealthStat : Stat {
 
     public HealthStat(int value) : base(value) {
         this.value = value;
     }
 
     public override string Print() {
         return "Health: " + value + "/n";
     }
 }
 
 [System.Serializable]
 public class HealthRegStat : Stat {
 
     public HealthRegStat(int value) : base(value) {
         this.value = value;
     }
 
     public override string Print() {
         return "Health regen: " + value + "/n";
     }
 }
 
 [System.Serializable]
 public class ShieldStat : Stat {
 
     public ShieldStat(int value) : base(value) {
         this.value = value;
     }
 
     public override string Print() {
         return "Shield: " + value + "\n";
     }
 }
 
 [System.Serializable]
 public class ShieldRegStat : Stat {
 
     public ShieldRegStat(int value) : base(value) {
         this.value = value;
     }
 
     public override string Print() {
         return "Shield Regen: " + value + "/n";
     }
 }
 
 [System.Serializable]
 public class MoveSpeedStat : Stat {
 
     public MoveSpeedStat(int value) : base(value) {
         this.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,
     Shield,
     Weapon,
     Chassis,
 }
 
 public class ShipPart : 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
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
0

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.

Follow this Question

Answers Answers and Comments

149 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

Related Questions

Dictionary in inspector 4 Answers

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

Custom Inspector - choosing subtype for a field 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