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 + "
";
}
}
[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: