vane xeer
vane xeer

Reputation: 11

Cannot convert Datatypes from-to

I have a class called DisplayInventory

Dictionary<InventoryObject, GameObject> itemsDisplayed = new Dictionary<InventoryObject, GameObject>();

itemsDisplayed.Add(inventory.Container[i], obj);

the code breaks at this line (inventory.Container[i]) because it cannot convert (field) List InventoryObject.Container.


this is my InventoryObject class

public class InventoryObject : ScriptableObject { public List Container = new List(); public void AddItem(ItemObjectData _item, int _amount) { bool hasItem = false; for (int i = 0; i < Container.Count; i++) { if(Container[i].item == _item) { Container[i].AddAmount(_amount); hasItem = true; break; } } if(!hasItem) { Container.Add(new InventorySlot(_item, _amount)); } }

}

[System.Serializable] public class InventorySlot { public ItemObjectData item; public int amount; public InventorySlot(ItemObjectData _item, int _amount) { item = _item; amount = _amount; }

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

Upvotes: 0

Views: 43

Answers (1)

Jules
Jules

Reputation: 11

The part where you declare the inventory variable or specifically inventory.container is missing. Or is this the container from the InventoryObject class?

You need the specific type InventoryObject for the Dictionary. What you are giving into it is simply just a List object that you put inside the container. If you are refering to the container from the InventoryObject class, it really is just a List() that you hold there.

In this case the Dictionary would only need inventory as input.

Or you could change from

public List Container = new List();

to this

public List<InventoryObject> Container = new List<InventoryObject>();

This would still make more sense if this container is outside of the InventoryObject and in an Inventory class or something.

Upvotes: 0

Related Questions