Forum begins after the advertisement:


[Part 10] ArgumentOutOfRangeException in upgrade

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 10] ArgumentOutOfRangeException in upgrade

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #12498
    Jaylen
    Level 10
    Participant
    Helpful?
    Up
    0
    ::

    Currently at Part 10 of the Vampire Survivor Tutorial. I am doing the upgrade system, however I get this error :

    ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <787acc3c9a4c471ba7d971300105af24>:0)
    InventoryManager.AddWeapon (System.Int32 slotIndex, WeaponController weapon) (at Assets/Script/Player/InventoryManager.cs:53)
    PlayerStats.SpawnWeapon (UnityEngine.GameObject weapon) (at Assets/Script/Player/PlayerStats.cs:201)
    PlayerStats.Awake () (at Assets/Script/Player/PlayerStats.cs:103)

    Not really sure where the issue is

    #12499
    Jaylen
    Level 10
    Participant
    Helpful?
    Up
    0
    ::

    nvm i fixed it

    #12500
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Glad you fix it Jaylen. Just to provide context for anyone else reading this, ArgumentOutOfRangeExceptions occur when you try to access arrays or list items with indexes beyond the length of the array. For example:

    int[] nums = new int[4];
    // This will cause an ArgumentOutOfRangeException because the array only has 4 items.
    nums[6] = 10;

    In Jaylen’s case, it’s probably caused by this chunk of code in InventoryManager (judging from his screenshot):

        public void AddWeapon(int slotIndex, WeaponController weapon)   //Add a weapon to a specific slot
        {
            weaponSlots[slotIndex] = weapon;
            weaponLevels[slotIndex] = weapon.weaponData.Level;
            weaponUISlots[slotIndex].enabled = true;   //Enable the image component
            weaponUISlots[slotIndex].sprite = weapon.weaponData.Icon;
        }

    Specifically, if you try to call AddWeapon() and pass a slotIndex of more than 5, it will cause the error.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: