- Home /
Inventory not working in android build
I'm making an inventory system for my 2D game. Every time I collided with an object it will be saved in my inventory. I am saving the inventory data in a JSON file. The problem is when first run the game and I collided with an objects it is not shown in my inventory but saved in my JSON file. It works perfectly fine in the editor but when I tested it on my device it doesn't. This is my code to add item upon collision:
public void GetItem(int id)
{
Item itemToAdd = itemDB.FetchItemByID(id);
if (itemToAdd.Stackable && IfItemIsInInventory(itemToAdd))
{
for (int i = 0; i < items.Count; i++)
{
if (items[i].ID == id)
{
ItemData data = slots[i].transform.GetChild(0).GetComponent<ItemData>();
data.amount++;
data.transform.GetChild(0).GetComponent<Text>().text = data.amount.ToString();
break;
}
}
}
else
{
for (int i = 0; i < items.Count; i++)
{
if (items[i].ID == -1)
{
items[i] = itemToAdd;
GameObject itemObj = Instantiate(inventoryItem);
itemObj.transform.SetParent(slots[i].transform);
itemObj.transform.position = Vector2.zero;
itemObj.GetComponent<Image>().sprite = itemToAdd.Sprite;
itemObj.name = itemToAdd.Title;
if (itemChangedCallBack != null)
itemChangedCallBack.Invoke();
break;
}
}
}
}
I'll be also attaching my whole inventory script for reference. Hope you can help me. Been working with for a long time. Thanks in advance, cheers ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220612144906im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flip over an object (smooth transition) 3 Answers
Drag and Drop icons into slots 1 Answer
check if list contains item with matching string property 2 Answers