I have a GameObject you can walk on to pick it up. This GameObject has an ItemContainer class extending from MonoBehviour. ItemContainer has an Item property, which does NOT extend from MonoBehaviour.
When I pick this GameObject up I look within the ItemContainer and retrieve the Item within this class. I add this Item by copying it to my Inventory which has a list of items. Once I’m done, I delete this GameObject as we’ve picked it up already.
As expected I have various items I’d like to see in this game so when I make a Medkit class extending the Item things become problematic.
The Medkit class can override methods like DoOnUse which should apply healing effects to the player, however, my ItemContainer doesn’t allow me to specify the type of Item in the Inspector. Eventually I can’t configure my Items to be a child of Item in the ItemContainer’s inspector.
If I got rid of the ItemContainer.cs and converted the Item class to extend from MonoBehaviour then adding the items to my inventory necessitates collecting them as GameObjects which is problematic if you have one hundred items of different types in your inventory.
If I used a Container for every child I have then that’s too many scripts you have to do duplicates for and feels like a dirty solution.
If I wrote an Editor class extending ItemContainer’s inspector then it’s a lot of effort for something so simple and again there are so many steps just to make a simple Item.
I humbly ask for your help.
ItemContainer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemContainer : MonoBehaviour
{
public Item item;
public void Awake()
{
SetGameObjectOfItem();
}
public virtual void SetGameObjectOfItem()
{
item.myGameObject = gameObject;
}
}
Item.cs
using UnityEngine;
[System.Serializable]
public class Item
{
[SerializeField]
public GameObject myGameObject;
[SerializeField]
public string uniqueID;
[SerializeField]
public string name;
[SerializeField]
public string pickUpText;
[SerializeField]
public bool isQuantifiable;
[SerializeField]
public int quantity;
public void IncrementQuantity()
{
quantity++;
}
public void DecrementQuantity()
{
quantity--;
}
[SerializeField]
public bool isEquippable;
public virtual void DoOnEquip(){}
public virtual void DoOnUnEquip(){}
public virtual void DoOnPickUp(){}
[SerializeField]
public bool isUsable;
public virtual void DoOnUse(){}
}
Medkit.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Medkit : Item
{
public override void DoOnUse()
{
base.DoOnUse();
//TODO: Insert healing code.
Debug.Log("Healing player!");
}
}