This code that I programmed is the begginings of an inventory system I’m making, and the Item Catalogue will contain every attainable item in the game, anyway this inventory AddItem is supposed to find out if theres already an item with this name, if there isn’t, add the item with its values to the inventory, but if theres already an item with the name, then add to the quantity, however, when I add to “ItemToAddTo” it also adds to the ItemCatalogue? And I can’t for the life of me figure out why? (The Item Catalogue script is just a list, nothing else, and the itemclass which is where the Items’ information is stored is just a class with no actual code besides a bunch of variables in it.
import System.Collections.Generic;
var ItemCatalogue;
var Inventory : List.<ItemClass> = new List.<ItemClass>();
var Weight : float;
function Start()
{
ItemCatalogue = GameObject.FindWithTag("GameLogic").GetComponent("Item Catalogue").Catalogue;
RecalWeight();
}
function Update()
{
}
function RecalWeight()
{
Weight = 0;
for each(var InventoryItem in Inventory)
{
Weight += InventoryItem.Weight * InventoryItem.Quantity;
}
}
function AddItem(Item : ItemClass)
{
for each(var InventoryItem in Inventory)
{
if(InventoryItem.Name == Item.Name)
{
var IsInInventory = true;
var ItemToAddTo = InventoryItem;
}
}
if(IsInInventory)
{
ItemToAddTo.Quantity += Item.Quantity;
}
else
{
Inventory.Add(Item);
}
IsInInventory = false;
RecalWeight();
}
function OnGUI()
{
if(GUI.Button(Rect(10, 70, 50, 30), "Items"))
{
AddItem(ItemCatalogue[0]);
}
}
Any ideas?