U
U
Uncle Bogdan2021-07-12 20:07:42
Unity
Uncle Bogdan, 2021-07-12 20:07:42

How to make an instance of a class not be a reference, but rather a value?

Tipo scriptable object and class is the same reference type. And there is a list of items and there is in order to add them to the ui and after creating the object, the Render method is called and there the Item parameter and there is a customItem field supposedly should inherit almost everything from item, but besides the customItem variables, it also changes the item variables.

If you don't understand, here is the code:

Inventory.Items.ForEach(item =>
        {
            var Amount = item.Amount;
            Item customItem = item;

            while(true)
            {
                if(Amount >= item.MaxAmount)
                {
                    customItem.Amount = item.MaxAmount;
                    Amount -= item.MaxAmount;

                    var cell = Instantiate(_inventotyCell, _contaner);
                    cell.Render(customItem);//new Item() { Amount = item.MaxAmount, Image = item.Image });
                }
                else
                {
                    customItem.Amount = Amount;
                    Amount = 0;

                    var cell = Instantiate(_inventotyCell, _contaner);
                    cell.Render(customItem);//new Item() { Amount = item.MaxAmount, Image = item.Image });
                }

                if(Amount == 0)
                {
                    break;
                }
            }
        });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-07-12
@motkot

Because this is Item customItem = item;not creating a new instance, but simply copying the link. If you need a new one, then you need to create it throughnew

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question