- Home /
How to make a copy of the data
SO, I have an item database where I store all my items I am creating into a list and then I have an inventory script which puts the info onto my icon's Data script. I created away to be able to stack the items up, but when I do, but it modifies the values on the icon as well in the Item Database. I want it to make a copy of my item data from my item data base and then put that onto my icon data, so when I stack my items, it only modifies the icon data and not the data from the item database.
Inventory Script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Inventory_2 : MonoBehaviour { public Transform SlotsHolder; public Slot_2[] Slots;
private List<Item> _organizer;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
AddItemtoInventory(new Item("", 1, 1, null, ItemType.Item));
}
if (Input.GetKeyDown(KeyCode.E))
{
AddItemtoInventory(new Item("", 2, 1, null, ItemType.Item));
}
}
private void AddItemtoInventory(Item _newItem)
{
Slots = SlotsHolder.GetComponentsInChildren<Slot_2>();
for (int i = 0; i < Slots.Length; i++)
{
if (Slots[i]._infoHolder.itemName == null)
{
for (int d = 0; d < ItemDataBase.Database.Items.Count; d++)
{
if (ItemDataBase.Database.Items[d].itemID == _newItem.itemID)
{
Slots[i].AddItem(ItemDataBase.Database.Items[d]);
}
}
break;
}
}
}
}
Slot Script: using System; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI;
public class Slot_2 : MonoBehaviour, IDropHandler { public Item _infoHolder;
public GameObject _itemIcon
{
get
{
if (transform.childCount > 0)
{
return transform.GetChild(0).gameObject;
}
else
{
return null;
}
}
}
private void Start()
{
AddItem(new Item());
}
private void Update()
{
if(_itemIcon != null)
{
_infoHolder = _itemIcon.GetComponent<IconData_2>()._itemInfo;
}
else
{
return;
}
if (_infoHolder.itemName != null)
{
_itemIcon.SetActive(true);
_itemIcon.GetComponent<Image>().sprite = _itemIcon.GetComponent<IconData_2>()._itemInfo.itemIcon;
_itemIcon.transform.GetComponentInChildren<Text>().text = _infoHolder.itemAmount.ToString();
}
else
{
_itemIcon.SetActive(false);
}
}
public void AddItem(Item _newItem)
{
_itemIcon.GetComponent<IconData_2>()._itemInfo = _newItem;
}
public void OnDrop(PointerEventData eventData)
{
if(eventData.button == PointerEventData.InputButton.Left)
{
if(_itemIcon == null)
{
IconDrag_2._icon.transform.SetParent(transform);
}
else
{
if (IconDrag_2._icon.GetComponent<IconData_2>()._itemInfo.itemName == _infoHolder.itemName)
{
_infoHolder.itemAmount += IconDrag_2._icon.GetComponent<IconData_2>()._itemInfo.itemAmount;
IconDrag_2._icon.GetComponent<IconData_2>()._itemInfo = new Item();
}
else
{
_itemIcon.transform.SetParent(IconDrag_2._icon._orginalParent);
IconDrag_2._icon.transform.SetParent(transform);
}
}
}
}
}