U
U
Uncle Bogdan2021-07-12 10:37:52
Unity
Uncle Bogdan, 2021-07-12 10:37:52

Why does the object not want to be deleted?

For some reason, it throws an error:

InvalidCastException: Specified cast is not valid.
Inventory.RenderItems () (at Assets/Scripts/Inventory.cs:17)
Inventory.OnEnable () (at Assets/Scripts/Inventory.cs:12)

Code:

public class Inventory : MonoBehaviour
{
    [SerializeField] private List<Item> Items;
    [SerializeField] private Transform _contaner;
    [SerializeField] private InventoryCell _inventotyCell;

    private void OnEnable()
    {
        RenderItems();
    }

    private void RenderItems()
    {
        foreach(GameObject child in _contaner)
        {
            Destroy(child);
        }

        Items.ForEach(item =>
        {
            var cell = Instantiate(_inventotyCell, _contaner);
            cell.Render(item);
        });
    }
}

Answer the question

In order to leave comments, you need to log in

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

Well, your container is a transform, and it goes through its transformer children. And it does not turn into a gameobject, as indicated by the error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question