Hello,
So i got ScriptableObject Chest and when i use it, it reference to original Chest asset so when i got 2 same chests in inventory it shows exacly same numbers and if i open one of them it’s destroying both. How can i copy my Chest in runtime and reference to a copy?
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
[CreateAssetMenu(menuName = "Items/ChestOfItems")]
public class Chest : Item
{
public Reward Reward;
public int howManyOpens;
[SerializeField] private int howManyItemsDrop;
public override void OnValidate()
{
base.OnValidate();
for (int i = 0; i < Reward.Items.Count; i++)
{
Reward.Items[i].dropChance = 100 / Reward.Items.Count;
}
}
public void Open(Character character)
{
howManyItemsDrop = Random.Range(Reward.minItemsToDrop, Reward.maxItemsToDrop);
if (character.inventory.FreeSpaces() >= howManyItemsDrop)
{
character.AddExp(Reward.experience);
character.AddCurrency(Reward.currency);
for (int i = 0; i < howManyOpens; i++)
{
List<ItemDropChance> items = new List<ItemDropChance>(Reward.Items);
int roll = Random.Range(0, 101);
Debug.Log(roll);
int itemChances = 0;
for (int j = 0; j < items.Count; j++)
{
itemChances += items[j].dropChance;
if (roll <= itemChances)
{
character.inventory.AddItem(items[j].item.GetCopy());
Debug.Log("Copied items lenght: " + items.Count);
items.RemoveAt(j);
break;
}
}
}
howManyOpens--;
}
}
public bool IsOpened()
{
return howManyOpens <= 0;
}
public override string GetDescription()
{
sb.Clear();
sb.Append("Opens left: " + howManyOpens);
sb.AppendLine();
sb.Append("Contains / Chance");
for (int i = 0; i < Reward.Items.Count; i++)
{
sb.AppendLine();
sb.Append(Reward.Items[i].item.ItemName + " / " + Reward.Items[i].dropChance +"%");
}
return sb.ToString();
}
public override void Destroy()
{
Destroy(this);
}
}