Skip to main content
Bumped by Community user
Bumped by Community user
edited title
Link

Unity - Array of cutomcustom class with a superclass using generics does not show in the inspector

added 25 characters in body
Source Link

I want to create 2 classes for Scriptable Object: Weapon and Armor. Because both have similar functionality, I made a base class for both of them.

using UnityEngine;

public abstract class Gear<T> : ScriptableObject{

    public int price;
    public Spriteint image;level;
    public stringSprite slug;image;

    [Header("Levels List")]
    public int level;
    public StatsLevel<T>[]T[] levels;

    public StatsLevel<T>T CurrentLevel() {
        return this.levels[this.level];
    }

    public StatsLevel<T>T NextLevel() {
        return this.levels[this.level + 1];
    }

    public bool UpgradesLeft() {
        return this.level + 1 < this.levels.Length;
    }

    public void Reset() {
        this.level = 0;
    }

    public bool Upgrade() {
        if (!this.UpgradesLeft()) return false;
        this.level++;
        return true;
    }
}


[System.Serializable]
public abstract class StatsLevel<T>StatsLevel {
    public int price;
    public T stats;
}

Both weapons and armors have the properties in common like they all have a price, they all have a Sprite Image, and so on... and more importantly they all have an array of Stat Levels. Meaning that every armor or weapon can have different levels, and depending on their level, the stats will be different. For that I used Generics.

So when I created the Armor, I did it this way:

using UnityEngine;

public enum ArmorStat { Low, Med, High, Super }

[CreateAssetMenu(fileName = "Armor", menuName = "MyGame"NARG/Armor", order = 1)]
public class Armor : Gear<ArmorStats>Gear<ArmorStatsLevel> {
    // OtherArmor custom code   logic
}

[System.Serializable]
public class ArmorStatsArmorStatsLevel : StatsLevel {
    public int weight;
    public ArmorStat blunt, slash, range;
}

in this class I define the stats unique to an Armor. Up to here everything looks peachy. But the issue comes with the Inspector:

enter image description here

As you can see, the inspector is not showing the array with the list of Stats on different levels, and I after a lot of research, I think it has to do with the Generics. If instead of unifying the common code between Armor and Weapon on the Base, I duplicate it each subclass, the Array is shown without problem.

How could I make it so that the array information is shown in the inspector?

I want to create 2 classes for Scriptable Object: Weapon and Armor. Because both have similar functionality, I made a base class for both of them.

using UnityEngine;

public abstract class Gear<T> : ScriptableObject{

    public int price;
    public Sprite image;
    public string slug;

    [Header("Levels List")]
    public int level;
    public StatsLevel<T>[] levels;

    public StatsLevel<T> CurrentLevel() {
        return this.levels[this.level];
    }

    public StatsLevel<T> NextLevel() {
        return this.levels[this.level + 1];
    }

    public bool UpgradesLeft() {
        return this.level + 1 < this.levels.Length;
    }

    public void Reset() {
        this.level = 0;
    }

    public bool Upgrade() {
        if (!this.UpgradesLeft()) return false;
        this.level++;
        return true;
    }
}


[System.Serializable]
public abstract class StatsLevel<T> {
    public int price;
    public T stats;
}

Both weapons and armors have the properties in common like they all have a price, they all have a Sprite Image, and so on... and more importantly they all have an array of Stat Levels. Meaning that every armor or weapon can have different levels, and depending on their level, the stats will be different. For that I used Generics.

So when I created the Armor, I did it this way:

using UnityEngine;

public enum ArmorStat { Low, Med, High, Super }

[CreateAssetMenu(fileName = "Armor", menuName = "MyGame/Armor", order = 1)]
public class Armor : Gear<ArmorStats> {
    // Other custom code   
}

[System.Serializable]
public class ArmorStats {
    public int weight;
    public ArmorStat blunt, slash, range;
}

in this class I define the stats unique to an Armor. Up to here everything looks peachy. But the issue comes with the Inspector:

enter image description here

As you can see, the inspector is not showing the array with the list of Stats on different levels, and I after a lot of research, I think it has to do with the Generics. If instead of unifying the common code between Armor and Weapon on the Base, I duplicate it each subclass, the Array is shown without problem.

How could I make it so that the array information is shown in the inspector?

I want to create 2 classes for Scriptable Object: Weapon and Armor. Because both have similar functionality, I made a base class for both of them.

using UnityEngine;

public abstract class Gear<T> : ScriptableObject{

    public int price;
    public int level;
    public Sprite image;

    [Header("Levels List")]
    public T[] levels;

    public T CurrentLevel() {
        return this.levels[this.level];
    }

    public T NextLevel() {
        return this.levels[this.level + 1];
    }

    public bool UpgradesLeft() {
        return this.level + 1 < this.levels.Length;
    }

    public void Reset() {
        this.level = 0;
    }

    public bool Upgrade() {
        if (!this.UpgradesLeft()) return false;
        this.level++;
        return true;
    }
}


[System.Serializable]
public abstract class StatsLevel {
    public int price;
}

Both weapons and armors have the properties in common like they all have a price, they all have a Sprite Image, and so on... and more importantly they all have an array of Stat Levels. Meaning that every armor or weapon can have different levels, and depending on their level, the stats will be different. For that I used Generics.

So when I created the Armor, I did it this way:

using UnityEngine;

public enum ArmorStat { Low, Med, High, Super }

[CreateAssetMenu(fileName = "Armor", menuName = "NARG/Armor", order = 1)]
public class Armor : Gear<ArmorStatsLevel> {
    // Armor custom logic
}

[System.Serializable]
public class ArmorStatsLevel : StatsLevel {
    public int weight;
    public ArmorStat blunt, slash, range;
}

in this class I define the stats unique to an Armor. Up to here everything looks peachy. But the issue comes with the Inspector:

enter image description here

As you can see, the inspector is not showing the array with the list of Stats on different levels, and I after a lot of research, I think it has to do with the Generics. If instead of unifying the common code between Armor and Weapon on the Base, I duplicate it each subclass, the Array is shown without problem.

How could I make it so that the array information is shown in the inspector?

Source Link

Unity - Array of cutom class with a superclass using generics does not show in the inspector

I want to create 2 classes for Scriptable Object: Weapon and Armor. Because both have similar functionality, I made a base class for both of them.

using UnityEngine;

public abstract class Gear<T> : ScriptableObject{

    public int price;
    public Sprite image;
    public string slug;

    [Header("Levels List")]
    public int level;
    public StatsLevel<T>[] levels;

    public StatsLevel<T> CurrentLevel() {
        return this.levels[this.level];
    }

    public StatsLevel<T> NextLevel() {
        return this.levels[this.level + 1];
    }

    public bool UpgradesLeft() {
        return this.level + 1 < this.levels.Length;
    }

    public void Reset() {
        this.level = 0;
    }

    public bool Upgrade() {
        if (!this.UpgradesLeft()) return false;
        this.level++;
        return true;
    }
}


[System.Serializable]
public abstract class StatsLevel<T> {
    public int price;
    public T stats;
}

Both weapons and armors have the properties in common like they all have a price, they all have a Sprite Image, and so on... and more importantly they all have an array of Stat Levels. Meaning that every armor or weapon can have different levels, and depending on their level, the stats will be different. For that I used Generics.

So when I created the Armor, I did it this way:

using UnityEngine;

public enum ArmorStat { Low, Med, High, Super }

[CreateAssetMenu(fileName = "Armor", menuName = "MyGame/Armor", order = 1)]
public class Armor : Gear<ArmorStats> {
    // Other custom code   
}

[System.Serializable]
public class ArmorStats {
    public int weight;
    public ArmorStat blunt, slash, range;
}

in this class I define the stats unique to an Armor. Up to here everything looks peachy. But the issue comes with the Inspector:

enter image description here

As you can see, the inspector is not showing the array with the list of Stats on different levels, and I after a lot of research, I think it has to do with the Generics. If instead of unifying the common code between Armor and Weapon on the Base, I duplicate it each subclass, the Array is shown without problem.

How could I make it so that the array information is shown in the inspector?