Inventory slots number are wrong
So I got a code for an inventory system. However something is wrong. Here's the code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Inventory1 : MonoBehaviour {
public List<GameObject> Slots = new List<GameObject> ();
public List<Item> Items = new List<Item> ();
public GameObject slots;
ItemDatabase database;
int x = -200;
int y = 195;
// Use this for initialization
void Start () {
int Slotamount = 0;
database = GameObject.FindGameObjectWithTag ("Item Database").GetComponent<ItemDatabase> ();
for (int i = 1; i < 7; i++) {
for(int k = 1; k < 4; k++){
GameObject slot = (GameObject)Instantiate(slots);
slots.GetComponent<SlotsScript>().slotNumber = Slotamount;
Slots.Add (slot);
Items.Add (new Item());
slot.transform.parent = this.gameObject.transform;
slot.name = "slot" + " " + i + "." + k;
slot.GetComponent<RectTransform>().localPosition = new Vector3(x,y,0);
x = x + 385;
if(k == 2){
x = -200;
y = y - 78;
}
Slotamount++;
}
}
AddItem (0);
AddItem (1);
}
void AddItem(int id){
for (int i = 0; i < database.items.Count; i++) {
if(database.items[i].itemID == id){
Item item = database.items[i];
AddItemAtEmptySlot(item);
break;
}
}
}
void AddItemAtEmptySlot(Item item){
for (int i = 0; i < Items.Count; i++) {
if(Items[i].itemName == null){
Items[i] = item;
break;
}
}
}
}
That's the inventory code, and here is the slots code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class SlotsScript : MonoBehaviour, IPointerDownHandler, IPointerEnterHandler{
public Item item;
Image itemImage;
public int slotNumber;
Inventory1 inventory1;
// Use this for initialization
void Start () {
inventory1 = GameObject.FindGameObjectWithTag ("Inventory1").GetComponent<Inventory1> ();
itemImage = gameObject.transform.GetChild (0).GetComponent<Image> ();
}
// Update is called once per frame
void Update () {
if (inventory1.Items[slotNumber].itemName != null) {
item = inventory1.Items[slotNumber].itemName;
itemImage.enabled = true;
itemImage.sprite = inventory1.Items[slotNumber].itemIcon;
} else {
itemImage.enabled = false;
}
}
public void OnPointerDown(PointerEventData data){
Debug.Log (slotNumber);
}
public void OnPointerEnter(PointerEventData data){
}
}
Now my problem is that my very first slot, named 1.1 gets the slot number 17. Then my second slot is 0 and so on till the last one that is 16. Because of this when I add items in the inventory, they start adding from the second slot. Can someone help me?
Create a Prefab of the slots and assign it in the inspector ins$$anonymous$$d of leaving it on Scene view. $$anonymous$$ake sure to set its Id to 0 before creating the prefab.
@Positive7 I actually did that. I assigned the prefab in the inspector. I see the slots and everything. $$anonymous$$y problem is that the first slot is seen as the last one. Therefore item stacking begins from the second slot.
Ohh... I remember using this script a long time ago and if I remember right that solved the issue for me. Then I had some more troubles with this Inventory so I decide it to build my own.
Just set it to 0 in the Inspector before creating the prefab. otherwise it will keep reseting to 17 or whatever number it was(as far as I remember).