Having a problem with an inventory script.
am sure its something to do with reference and clone. but i have no idea how to sort it.
been trying random things but nothing fixes it.
basically when i get to the max stack size of 5 i want it to create a new stack at 1/5 but instead it keeps the 5/5 stack count when it makes the new one.
code below any help would be great been at it all day heh.
Sorry if its abit messy only been at this c# stuff a few days.
INVENTORY Script
public class Inventory : MonoBehaviour {
public List<GameObject> Slots = new List<GameObject>();
public List<Item> Items = new List<Item>();
public GameObject slots;
public GameObject toolTip;
public GameObject draggeditemgameobject;
public bool draggingItem = false;
public Item draggedItem;
public int indexofdraggeditem;
ItemDatabase database;
void Update()
{
if (Input.GetMouseButtonDown(1))
{
addItem(0);
Debug.Log("key pressed");
}
if (draggingItem)
{
Vector3 posi = (Input.mousePosition - GameObject.FindGameObjectWithTag("Canvas").GetComponent<RectTransform>().localPosition);
draggeditemgameobject.GetComponent<RectTransform>().localPosition = new Vector3(posi.x + 25, posi.y - 15, posi.z);
}
}
public void showToolTip(Vector3 toolPos, Item item)
{
toolTip.SetActive(true);
toolTip.GetComponent<RectTransform>().localPosition = new Vector3(toolPos.x + 742, toolPos.y, toolPos.z);
toolTip.transform.GetChild(0).GetComponent<Text>().text = item.itemName;
toolTip.transform.GetChild(1).GetComponent<Text>().text = item.itemDesc;
}
public void closeTooltip()
{
toolTip.SetActive(false);
}
public void showDraggedItem(Item item, int slotNumber)
{
indexofdraggeditem = slotNumber;
closeTooltip();
draggeditemgameobject.SetActive(true);
draggingItem = true;
draggedItem = item;
draggeditemgameobject.GetComponent<Image>().sprite = item.itemIcon;
}
public void closeDraggedItem ()
{
draggingItem = false;
draggeditemgameobject.SetActive(false);
}
// Use this for initialization
void Start () {
int slotAmount = 0;
database = GameObject.FindGameObjectWithTag("ItemDatabase").GetComponent<ItemDatabase>();
for (int i = 1; i < 7; i++)
{
for (int k = 1; k < 7; k++)
{
GameObject slot = (GameObject)Instantiate(slots);
slot.GetComponent<SlotScript>().slotNumber = slotAmount;
Slots.Add(slot);
Items.Add(new Item());
slot.name = "slot" + i + "." + k;
slot.transform.SetParent (this.gameObject.transform,true);
slotAmount++;
}
}
addItem(1);
addItem(0);
addItem(0);
addItem(0);
addItem(1);
}
void AddItemAtEmptySlot(Item item)
{
for (int k = 0; k < Items.Count; k++)
{
if (Items[k].itemName == null)
{
Items[k] = item;
break;
}
}
}
public void checkIfItemExist(int itemID, Item item)
{
for (int i = 0; i < Items.Count; i++)
{
if (Items[i].itemID == itemID && Items[i].itemValue != Items[i].itemMaxStack)
{
Debug.Log("running if");
Items[i].itemValue = Items[i].itemValue + 1;
break;
}
else if (i == Items.Count - 1 && Items[i].itemValue == Items[i].itemMaxStack)
{
Debug.Log("running else");
AddItemAtEmptySlot(item);
}
}
}
void addItem(int id)
{
for (int k = 0; k < database.items.Count; k++)
{
if (database.items[k].itemID == id)
{
Item item = database.items[k];
checkIfItemExist(id, item);
}
}
}
ITEMDB SCRIPT
public List<Item> items = new List<Item>();
void Start ()
{
//string name, int id, string desc, int power, int thirst, int hunger, int health, int value, ItemType type
items.Add(new Item ("Green Goo", 0, "Default Desc", 0, 0, 0, 0, 1, 5, Item.ItemType.Consumable));
items.Add(new Item ("Weak Dagger", 1, "A weak dagger, looks to have been well used.", 20, 0, 0, 0, 1, 1, Item.ItemType.Weapon));
items.Add(new Item ("c_Apple", 2, "A Rotten Apple", 0, 5, 10, 0, 1, 10, Item.ItemType.Consumable));
items.Add(new Item("c_Water", 3, "Water Bottle", 0, 10, 5, 0, 1, 20, Item.ItemType.Consumable));
}
ITEM SCRITP
[System.Serializable]
public class Item {
public string itemName;
public int itemID;
public string itemDesc;
public Sprite itemIcon;
public int itemPower;
public int itemThirst;
public int itemHunger;
public int itemHealth;
public int itemValue;
public int itemMaxStack;
//public GameObject itemModel;
public ItemType itemType;
public enum ItemType
{
Weapon,
Consumable,
Buildable,
Clothing
}
public Item(string name, int id, string desc, int power, int thirst, int hunger, int health, int value, int maxstack, ItemType type)
{
itemName = name;
itemID = id;
itemDesc = desc;
itemPower = power;
itemThirst = thirst;
itemHunger = hunger;
itemHealth = health;
itemType = type;
itemValue = value;
itemMaxStack = maxstack;
itemIcon = Resources.Load<Sprite>("" + name);
// itemModel = Resources.Load<GameObject>("DroppedItem");
}
public Item()
{
}