- Home /
adding inventory items is half-working
Hello all, I'm really struggling with this one, please help me. First, let me show you the code.`
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour
{
GameObject inventoryPanel;
GameObject slotPanel;
itemDataBase database;
public GameObject inventorySlot;
public GameObject inventoryItem;
int slotAmount;
public List<Item> items = new List<Item>();
public List<GameObject> slots = new List<GameObject>();
void Start()
{
database = GetComponent<itemDataBase> ();
slotAmount = 36;
inventoryPanel = GameObject.Find ("Inventory Panel");
slotPanel = inventoryPanel.transform.FindChild ("Slot Panel").gameObject;
for (int i = 0; i < slotAmount; i++)
{
items.Add (new Item ());
slots.Add (Instantiate (inventorySlot));
slots [i].transform.SetParent (slotPanel.transform);
}
AddItem (5);
}
public void AddItem(int id)
{
Item itemToAdd = database.FetchItemByID (id);
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 = new Vector2(0,0);
itemObj.GetComponent<Image>().sprite = itemToAdd.Sprite;
itemObj.name = itemToAdd.Title;
break;
}
}
}
}
This is the Inventory class, as you can see, I called "AddItem" with the ID=5 in Start(). In the first image that I uploaded, you can see that the item indeed shows in my inventory. However, I don't want to call this function here. Let me paste the second code.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class axePickUp : MonoBehaviour {
Inventory inventory;
void Start () {
inventory = GetComponent<Inventory> ();
}
void Update(){
if (Input.GetButtonDown("F")) {
pickUp();
}
}
void pickUp(){
inventory.AddItem (5);
}
}
I want to call this function in axePickUp(). The function kinda knows what I want, but not exactly, as you can see in the second image I uploaded....
The sprite image just sits there in the corner. PLEASE FOR THE LOVE OF GOD, help me. I have absolutely no idea how to fix this, I tried for days.
It's a bit hard to see where is the icon exactly in the second image, but here's an other one:
found the solution! It took me several hours, and my brothers help, but here is the code, we had to change several lines:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class Inventory : $$anonymous$$onoBehaviour
{
GameObject slotPanel;
itemDataBase database;
public GameObject inventorySlot;
public GameObject inventoryItem;
int slotAmount;
public List<Item> items = new List<Item>();
public List<GameObject> slots = new List<GameObject>();
void Start()
{
database = GetComponent<itemDataBase> ();
slotAmount = 36;
slotPanel = GameObject.Find ("Slot Panel");
for (int i = 0; i < slotAmount; i++)
{
items.Add (new Item ());
slots.Add (Instantiate (inventorySlot));
}
}
public void AddItem(int id)
{
Item itemToAdd = database.FetchItemByID (id);
for (int j = 0; j < slotAmount; j++) {
if (items [j].ID == -1) {
slots [j].transform.SetParent (slotPanel.transform);
items [j] = itemToAdd;
GameObject itemObj = Instantiate (inventoryItem);
itemObj.GetComponent<ItemData> ().item = itemToAdd;
itemObj.transform.SetParent (slots [j].transform);
itemObj.GetComponent<Image> ().sprite = itemToAdd.Sprite;
itemObj.name = itemToAdd.Title;
break;
}
}
}
}
This solved the problem above, however it's not working perfectly, because it's not loading the slots on Start, but if I add an item to it, for example if I kill a zombie, and it's loot is a sword and a pair of gloves, then it puts it in my inventory, and it shows it properly. The slot amount is 36, I can't carry more items than that, so it's not broken either. summa summarum it's working really well, I don't even care if it doesn't show the slots on start, it's not that necessary, I can live with that.
picture of it: http://kepfeltoltes.hu/160817/inv4_www.kepfeltoltes.hu_.png (for some reason I can't upload it here)