[CLOSED] When I use this code that I made, it adds to the item catalogue too? [CLOSED]

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?

Its because your adding the item on its self, you need to create a new instance of the item to add it. your setting up a reference to the item catalog thus when you add the quantity, it duplicates. use the instantiate command and it will crate a new instance of the item out of the catalog .

import System.Collections.Generic;
 

var Inventory : List.<ItemClass> = new List.<ItemClass>();
var ItemCatalogue;
var Weight : float;
 
 
 
function Start()
{
    ItemCatalogue = gameObject.GetComponent("ItemList").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(Instantiate(ItemCatalogue[0]));
    }
}

Interesting structure you have, less of a head ache that database every item and using its id to determine quantity. hope it helps