Here is my code....I have a feeling it is to do with my Items1 list not having anything in it, but I don't actually know how to add them to it! Am a noob and got all this from a youtube video.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class CraftField : MonoBehaviour {
public List Slots1 = new List();
public List- Items1 = new List
- (); //Taken from inventory
public GameObject slotsTest;
ItemDatabase database;
int x = -400;
int y = 167;
void Start () {
database = GameObject.FindGameObjectWithTag("ItemDatabase").GetComponent
();
for(int i = 1; i < 6; i++){
for(int k = 1; k < 6; k++){
GameObject slot1 = (GameObject)Instantiate(slotsTest);
Slots1.Add(slotsTest);
slot1.transform.parent = this.gameObject.transform;
slot1.name = "SlotTest" + i + "." + k;
slot1.GetComponent().localPosition = new Vector3(x, y, 0f);
x = x + 200;
if(k == 5){
x = -400;
y = y - 90;
}
}
}
}
public void checkIfItemExists(int itemID, Item item){
for (int i = 0; i < Items1.Count; i++){
if(Items1[i].itemID == itemID){
Items1[i].itemValue = Items1[i].itemValue + 1;
break;
} else if(i == Items1.Count - 1){
addItemAtEmptySlot(item);
}
}
}
public void addItem(int id){
for(int i = 0; i < database.items.Count; i++){
if(database.items[i].itemID == id){
Item item = database.items[i];
if(database.items[i].itemType == Item.ItemType.Consumable){
checkIfItemExists(id, item);
break;
} else{
addItemAtEmptySlot(item);
}
}
}
}
public void addItemAtEmptySlot(Item item){
for(int i = 0; i < Items1.Count; i++){
if(Items1[i].itemName == null){
Items1[i] = item;
break; //the loop does not need to continue so we break it
}
}
}
}
↧