A simple question about inheritance.

I’m redesigning the way stat modifiers from items work in my game and I’ve run into a little problem that I’m sure is just down to my being pretty new to writing scripts.

I have a variable data on this class:

public abstract class ItemObject : ScriptableObject
{
    #region Variables
    public Item data = new Item();
    public Sprite uiDisplay;
    public bool equippable = false;
    public bool stackable = false;
    public bool usable = false;
    public bool consumable = false;
    public ItemType type;
    [TextArea(15, 20)]
    public string description;
    #endregion

    public abstract void Awake();
    public abstract Item CreateItem();
}

and a variable data on this class that inherits from it:

public class WeaponObject : EquipmentObject
{
    public new Weapon data = new Weapon();
    public override Item CreateItem()
        {
            Weapon newItem = new Weapon(this);
            return newItem;
        }
}

It’s creating two instances of “Data” in the inspector instead of one. I want the script WeaponObject to replace data from ItemObject with its own rather than creating a new one. Because otherwise I am currently getting an error from this class:

[System.Serializable] public class Weapon : Item
{
    public ItemOffensiveStat[] itemOffensiveStats;

    public Weapon()
    {

    }
    public Weapon(WeaponObject item)
    {
        Id = item.data.Id;
        for (int i = 0; i < itemOffensiveStats.Length; i++)
        {
            itemOffensiveStats[i] = new ItemOffensiveStat(item.data.itemOffensiveStats[i].value)
            {
                type = item.data.itemOffensiveStats[i].type
            };
        }
    }
}

Telling me that item.data.itemOffensiveStats doesn’t exist.
For completeness here is the Item class Weapon inherits from:
```csharp

  • public Item(ItemObject item)
    {
    #region Variables
    Name = item.name;
    Id = item.data.Id;
    #endregion
    }
    }*
    ```

How can I do this? Forgive my asking such a simple question but I’ve been searching around a bit and I must not know the right keywords for finding this information.

To clarify what I’m actually trying to accomplish: the itemOffensiveStats is an array that holds my stats. As my system currently works it is in the ItemObject class, meaning that every item has Offensive Stats. I wanted make it such that only weapons have Offensive Stats so I’m trying to put itemOffensiveStats in my WeaponObject class. I would then like to put other arrays that are currently in my system onto Armor and so on in the same fashion instead of having Offensive Stats, Defensive Stats, Attributes etc. on all items as I currently do.

You can’t replace variables from a base class; you can only add new ones. Overriding only applies to functions, not variables.

2 Likes