1
\$\begingroup\$

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?

\$\endgroup\$
1
\$\begingroup\$

I've never worked with Unity assets so maybe I'm totally wrong here, but after pasting your code in an empty project and creating an Armor instance via Assets/Create/NARG/Armor, the inspector showed me everything:

screenshot

Tested on Unity 5.6.1f1

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.