Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question IndexOutOfRangeException ; why that is not working?

Discussion in 'Scripting' started by simiji, Jul 28, 2022.

  1. simiji

    simiji

    Joined:
    Sep 21, 2017
    Posts:
    8
    Well, I am quit lost.

    I wrote this code and I am really close to finish it.

    I just need to reload the cards when we finish the game... buuut, it is not working.

    And I do not know why... Can you help me?

    I have this part for instantiate the card :

    Code (CSharp):
    1. public void NewCard()
    2.         {
    3.             GameObject clone = (GameObject)Instantiate(SetOfCards[number], PointSpawn[0].position, Quaternion.identity);
    4.             number=number+1;
    5.             clone.name = ("carte");
    6.             clone.GetComponent<BoxCollider2D>().enabled = false;
    7.             clone.transform.SetParent(Bouclier.transform);
    8.         }
    9.  
    This for shuffle my array of cards :

    Code (CSharp):
    1. public void Shuffle()
    2.         {
    3.              for (int i = 0; i < SetOfCards.Length; i++)
    4.              {
    5.                  int rnd = Random.Range(0, SetOfCards.Length);
    6.                  tempGO = SetOfCards[rnd];
    7.                  SetOfCards[rnd] = SetOfCards[i];
    8.                  SetOfCards[i] = tempGO;
    9.              }
    10.          }      
    This is the function when we click on the button :

    Code (CSharp):
    1.  public void RunThisTask()
    2.         {
    3.  
    4.           Shuffle();
    5.           NewCard();
    6.           ShuffleDeck();
    7.           CreateDeck();
    8.          
    9.         }
    aaaaand here is my nightmare :

    IndexOutOfRangeException: Index was outside the bounds of the array.
    InstantiationExample.NewCard () (at Assets/Scripts/InstantiationExample.cs:82)
    InstantiationExample.RunThisTask () (at Assets/Scripts/InstantiationExample.cs:51)
    UnityEngine.Events.InvokableCall.Invoke () (at <e414e10bfe5f45729ff122f3359de21b>:0)
    UnityEngine.Events.UnityEvent.Invoke () (at <e414e10bfe5f45729ff122f3359de21b>:0)
    UnityEngine.UI.Button.Press () (at C:/Program Files/Unity/Hub/Editor/2020.2.0f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
    UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2020.2.0f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
    UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2020.2.0f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
    UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/Program Files/Unity/Hub/Editor/2020.2.0f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:262)
    UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Hub/Editor/2020.2.0f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:385)
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    It's an index out of range error. And it tells you, line 82 within the NewCard method.
    Even though your line numbers aren't there in what you pasted, there are two places you are trying to access an index.

    Code (CSharp):
    1.  GameObject clone = (GameObject)Instantiate(SetOfCards[number], PointSpawn[0].position, Quaternion.identity);
    So either SetOfCards[number] has a number value higher than the last index, or PointSpawn[0] is trying to target an empty collection, thus even index 0 doesn't exist. Now you need to solve which and figure out why it's out of range.

    Most likely, it's number.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    So that means you (OP) are most of the way to solving your problem... GO!

    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

    http://plbm.com/?p=236

    Steps to success:

    - find which collection it is (critical first step!)
    - find out why it has fewer items than you expect
    - fix whatever logic is making the indexing value exceed the collection
    - remember you might have more than one instance of this script in your scene/prefab

    It's just an error. You don't have nightmares about errors. You reason about them, you investigate them, and you fix them. The more you fix errors, the better you get at it, it becomes just like tapping backspace on a keyboard, just part of working.
     
  4. simiji

    simiji

    Joined:
    Sep 21, 2017
    Posts:
    8
    Woooow, I found it! It was so easy o_O

    I had a loop that instantiated my cards, but to make sure that the batch stayed at the maximum of 3 cards, I added a variable. Except that I had forgotten to set this variable to zero so it couldn't find the cards because it thought they had all been instantiated already

    I just wrote number=0; and now everything is working :D
     
  5. simiji

    simiji

    Joined:
    Sep 21, 2017
    Posts:
    8
    Thank you so much ^^
     
    Kurt-Dekker likes this.