D
D
Daniel24112021-06-20 16:32:49
C++ / C#
Daniel2411, 2021-06-20 16:32:49

Unit error: IndexOutOfRangeException: Index was outside the bounds of the array?

Hello, I was creating an inventory for a game and ran into this error: "IndexOutOfRangeException: Index was outside the bounds of the array."

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class sobiraenpredmetu : inventoryipob
{
    private inventoryipob inventoey;
    public GameObject slotButton;

    private void Start()
    {
        inventoey = GameObject.FindGameObjectWithTag("Player").GetComponent<inventoryipob>();
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            for (int i = 0; i < inventoey.slots.Length; i++)
            {
                if(idfull[i] == false)
                {
                    idfull[i] = true;
                    Instantiate(slotButton, slots[i].transform);
                    Destroy(gameObject);
                    break;
                }
            }
        }
    }
}


Looked at several sites on the Internet, but did not understand.

Ps. Please explain in detail, as I am new to this matter.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rundll32, 2021-06-20
@rundll32

The error itself says that you are accessing an index that is larger than the size of the array. It is not clear in your code how you create inventory, but the error is either in slots or idfull (which is more likely).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question