Hi, I have a problem with “Add Item” to inventory.
My code work but my item is not displayed on my inventory.
and i can’t fine solution, I am a beginer and all of the time i try understand how it works.
My code :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Surowce : MonoBehaviour {
public ItemDataBase database;
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.tag == "Player")
{
Inventory inventory = other.gameObject.GetComponent<Inventory> ();
if (inventory)
{
inventory.AddItem(3);
}
}
Debug.Log ("Dodaje Deske");
Destroy (gameObject);
}
}
When the character comes into contact with the object:
a) Object is deleted
b) Item is Added to inventory
Button " i " open my inventory where I can see what I have there.
But the item is not added there, and this is my problem.
I don’t know if you ask about this:
"
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour
...
void Awake ()
{
database = GameObject.FindGameObjectWithTag ("Item DataBase").GetComponent<ItemDataBase>();
for (int i = 0; i < (slotsX * slotsY); i++)
{
inventory.Add (new Item());
}
...
public void AddItem(int id)
{
for (int i = 0; i < inventory.Count; i++)
{
if (inventory[i].itemName == null)
{
for (int j = 0; j < database.items.Count; j++)
{
if (database.items[j].itemID == id)
inventory[i] = database.items[j];
}
break;
}
}
}