0

idk what I need to do to be inside the bounds of my array. i think my array is stuck not counting at all or counting to much between my 2 interfaces but idk if thats even the problem with it i just know its an array problem in my CreateSlots don't know if this helps but I'm following a unity tutorial called Unity3D equipping items scriptable object inventory system 'the code':

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

public class StaticInterface : UserInterface
{
    public GameObject[] Slots;

    public override void CreateSlots()
    {
        itemsDisplayed = new Dictionary<GameObject, InventorySlot>();
        for (int i = 0; i < inventory.Container.Items.Length; i++)
        {
            var obj = Slots[i]; //this line is the one unity pointed out

            AddEvent(obj, EventTriggerType.PointerEnter, delegate { OnEnter(obj); });
            AddEvent(obj, EventTriggerType.PointerExit, delegate { OnExit(obj); });
            AddEvent(obj, EventTriggerType.BeginDrag, delegate { OnDragStart(obj); });
            AddEvent(obj, EventTriggerType.EndDrag, delegate { OnDragEnd(obj); });
            AddEvent(obj, EventTriggerType.Drag, delegate { OnDrag(obj); });


            itemsDisplayed.Add(obj, inventory.Container.Items[i]);
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;

public abstract class UserInterface : MonoBehaviour
{

    public Player player;

    public InventoryObject inventory;
    public Dictionary<GameObject, InventorySlot> itemsDisplayed = new Dictionary<GameObject, InventorySlot>();
    void Start()
    {
        CreateSlots();
    }

    // Update is called once per frame
    void Update()
    {
        UpdateSlots();
    }

    public abstract void CreateSlots();//this line is the one unity pointed out
GrollyGames
  • 11
  • 1
  • 2

1 Answers1

0

I think your problem is, that the size of the inventory.Container.Items.Length is greater than the size of your Slots array so then Unity tries to get a value from the array, which doesn't exist. So maybe check, that the size of i is not greater than the Slots array using:

if(i < Slots.Lenght)
    var obj = Slots[i];
    ...
FrozenAssassine
  • 1,392
  • 1
  • 7
  • 16
  • hey can u further explain it pls i dont understand what you mean ima lil slow – GrollyGames Nov 10 '21 at 21:52
  • So lets say your array has 9 item in it, and you want to get the first item, you do this by writing arr[0]. In your case you tried to get an item which doesn't exist in the array, because you tried to get an item with a taller index than the array has items. You tried something like this arr[10] which would probably not work because your array only has 9 items. This is why you got index out of range The code i wrote you only checks if your variable i from the for loop is in the bounds of the array. – FrozenAssassine Nov 10 '21 at 22:06
  • You can also check this out: https://learn.microsoft.com/en-US/dotnet/api/system.indexoutofrangeexception?view=net-5.0 – FrozenAssassine Nov 10 '21 at 22:09