Forum begins after the advertisement:


[Part 16] IndexOutOfRangeException when selecting level 2 passive

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 16] IndexOutOfRangeException when selecting level 2 passive

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #14641

    Hello again Terence, whenever I select the second level of a passive item I get the following error:

    IndexOutOfRangeException: Index was outside the bounds of the array.
    PassiveData.GetLevelData (System.Int32 level) (at Assets/Scripts/Pasivas/PassiveData.cs:15)
    Passive.LevelUp () (at Assets/Scripts/Pasivas/Passive.cs:38)
    PlayerInventory.LevelUpPassive (System.Int32 slotIndex, System.Int32 upgradeIndex) (at Assets/Scripts/Jugador/PlayerInventory.cs:255)
    PlayerInventory+<>c__DisplayClass21_3.<ApplyUpgradeOptions>b__3 () (at Assets/Scripts/Jugador/PlayerInventory.cs:355)
    UnityEngine.Events.InvokableCall.Invoke () (at <f7237cf7abef49bfbb552d7eb076e422>:0)
    UnityEngine.Events.UnityEvent.Invoke () (at <f7237cf7abef49bfbb552d7eb076e422>:0)
    UnityEngine.UI.Button.Press () (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
    UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
    UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
    UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction
    1[T1] functor) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
    UnityEngine.EventSystems.EventSystem:Update() (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:530)

    Do you know what could be causing this? This is the line where the exception is being thrown:

    public Passive.Modifier GetLevelData(int level)
        {
            if (level - 2 < growth.Length)
            {
                return growth[level - 2];
            }
    
            Debug.LogWarning(string.Format("Passive doesn't have level up stats configured for level {0}", level));
            return new Passive.Modifier();
        }

    But if I click again on the level, it actually applies… is there something I’m missing?

    Also I wanted to ask if the passives stats are being changed as of now it does add a number, for example I have 5 moveSpeed and wings give “10%” but actually only adds 0.10 to 5, being now 5.1. Thanks in advance

    #14644
    Terence
    Keymaster

    Hi Alejandro, the error is in this line:

    return growth[level - 2];

    It will occur either when:

    1. level - 2 results in a value less than 0, or;
    2. the growth array does not have enough entries (e.g. you try retrieving data for level 5, but you only have data for 1 level)

    For the move speed stat, it just gets added with the code in Part 16. In Part 18, this is updated so that it is more similar to how Vampire Survivors handles it, i.e. percentage-based boosts.

    #14660

    In what cases could the level – 2 return less than 0? Also this is the passive data:

    View post on imgur.com

    #14662
    Terence
    Keymaster

    In what cases could the level – 2 return less than 0?

    If your level is less than 2, then level - 2 will be less than 0.

    Slipped my mind to mention this previously, but you can add a line of code to fix this:

    public Passive.Modifier GetLevelData(int level)
        {
            if (level <= 1) return baseStats;
    
            if (level - 2 < growth.Length)
            {
                return growth[level - 2];
            }
    
            Debug.LogWarning(string.Format("Passive doesn't have level up stats configured for level {0}", level));
            return new Passive.Modifier();
        }

    This is mentioned in the newly-released Part 19.

    Your passive data looks fine.

    #14679

    Hello thanks, it worked ^^ Appreciate the help and effort you putting in this tutorials

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

Go to Login Page →


Advertisement below: