public class Item : MonoBehaviour
{
public int itemQuantity;
[HideInInspector]
public int quantity;

    private void Start()
    {
        quantity = itemQuantity;
    }

    public virtual void ActionKeyDown(KeyCode key, Inventory inv)
    {

    } 
}

This is my Item class. When I run the game for the first time in the editor, it initializes with the correct quantity value (2). I use up all the of the item. Then, I quit and then run the game again. The quantity is still at 0.

Can someone please explain this strange behavior?

Sorry, but none of these are working. Every time, the items firmly remain at 0. I need help with this, because items are kinda critical in my game.

Nevermind, I figured it out by myself.
Thanks for trying to help! :slight_smile:

Edit: Here is the Item script.

public int itemQuantity;
    private int quantity;

    public virtual void ActionKeyDown(KeyCode key, Inventory inv)
    {
        
    }

    public virtual void ActionKey(KeyCode key, Inventory inv)
    {

    }

    public void RemoveAmount(int amount)
    {
        if (quantity - amount > -1)
        {
            quantity -= amount;
        }
    }

    public void AddAmount(int amount)
    {
        quantity += amount;
    }

    public int GetQuantity()
    {
        return quantity;
    }

This is the Inventory script.

    public List<Item> inventory;
    public int index;
    [HideInInspector]
    public Item currentItem;
    public Text text;
    public Text quantityText;
    public KeyCode[] keys;

    private void Start()
    {
        index = 0;
        foreach (Item item in inventory)
        {
            item.RemoveAmount(item.GetQuantity());
            item.AddAmount(item.itemQuantity);
        }
    }

    private void Update()
    {
        currentItem = inventory[index];

        if (Input.GetKeyDown(KeyCode.Alpha1))
            index = 0;

        if (Input.GetKeyDown(KeyCode.Alpha2))
            index = 1;

        if (Input.GetKeyDown(KeyCode.Alpha3))
            index = 2;

        if (Input.GetKeyDown(KeyCode.Alpha4))
            index = 3;

        if (currentItem != null)
        {
            text.text = currentItem.gameObject.name;
            quantityText.text = currentItem.GetQuantity().ToString();
        }
        else
        {
            text.text = "None";
            quantityText.text = "n/a";
        }


        if (currentItem != null && currentItem.GetQuantity() != 0)
        {
            foreach(KeyCode key in keys)
            {
                if (Input.GetKeyDown(key))
                {
                    currentItem.ActionKeyDown(key, this);
                }

                if (Input.GetKey(key))
                {
                    currentItem.ActionKey(key, this);
                }
            }

            //if (Input.GetKeyDown(KeyCode.Q))
            //{
            //    DropItem(index);
            //}
        }  
    }

    public bool AddItem(Item item, int slot)
    {
        if (inventory[slot] == null)
        {
            inventory[slot] = item;
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool AddItem(Item item)
    {
        if (currentItem == null)
        {
            currentItem = item;
            return true;
        }
        else
        {
            return false;
        }
    }

    public void RemoveItem(int slot)
    {
        inventory[slot] = null;
    }

    public void DropItem(int item)
    {
        if (inventory[item] != null)
        {
            cannonScript.Launch(GetComponent<playerReferences>().emitter, inventory[item].GetComponent<Rigidbody>(), 0);
            RemoveItem(item);
        }
    }

When the game starts, the inventory goes through all of the items in it and sets their quantity to 0. Then it adds their itemQuantity to their quantity. This is how I fixed it.