ArgumentOutOfRangeException, i dont understand why

Why get i this error? I know what the error means, but i dont know why 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.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <9577ac7a62ef43179789031239ba8798>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <9577ac7a62ef43179789031239ba8798>:0)
System.Collections.Generic.List1[T].get_Item (System.Int32 index) (at <9577ac7a62ef43179789031239ba8798>:0) GameManager+<>c__DisplayClass70_0.<Klondike_SetUpMatchfield>b__0 () (at Assets/Scripts/GameManager.cs:509) UnityEngine.Events.InvokableCall.Invoke () (at <e5b75411c8cd4bfaa9e760be20206f29>:0) UnityEngine.Events.UnityEvent.Invoke () (at <e5b75411c8cd4bfaa9e760be20206f29>:0) UnityEngine.UI.Button.Press () (at C:/Program Files/Unity/Hub/Editor/2019.4.13f1/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/2019.4.13f1/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/2019.4.13f1/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+EventFunction1[T1] functor) (at C:/Program Files/Unity/Hub/Editor/2019.4.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Hub/Editor/2019.4.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)

for (int i = 0; i < nachziehstapel.Count; i++)
        {
            nachziehstapel[i].SetCard(Instantiate(nachziehstapel[i].GetCard(), Matchfield.transform.Find("nachziehstapel").transform));
            nachziehstapel[i].SetBackside(Instantiate(nachziehstapel[i].GetBackside(), Matchfield.transform.Find("nachziehstapel").transform));
            nachziehstapel[i].GetBackside().GetComponent<Button>().onClick.AddListener(delegate { Klondike_Nachziehstapel_OpenCard(nachziehstapel[i].GetBackside()); });
           
            nachziehstapel[i].GetBackside().transform.localScale = new Vector2(0.15f, 0.15f);
            nachziehstapel[i].GetCard().transform.localScale = new Vector2(0.15f, 0.15f);

            nachziehstapel[i].GetCard().SetActive(false);
            nachziehstapel[i].GetBackside().SetActive(false);
            nachziehstapel[i].GetCard().transform.localPosition = new Vector2(klondike_nachziehstapel_x, klondike_nachziehstapel_y);
            nachziehstapel[i].GetBackside().transform.localPosition = new Vector2(klondike_nachziehstapel_x, klondike_nachziehstapel_y);
        }

Since you know what it means, you should debug your values, but you only showed a loop, so no clue what part the error actually points to in that code. But it’s line 509 of GameManager.

Line 509 of GameManager is Line 5 in the Loop.
I dont understand why i get trouble with the List, i acces to the GameObejcts in the Arrays in the other Lines with the variable “i” too. So why i get only in this Line an Error. I tried to debug about 2 hours, but i can’t fix the error.

Stacktrace tells us it’s the listener, not the executing loop.

Line 5 captures your iteration variable ‘i’ which ends up being equal to ‘nachziehstapel.Count’ when the loop finishes.
Since delegates and lambdas are closed over variables ( not their values !) all the listeners see the same value that was most recently assigned to that particular variable.
In other words: when the button listeners are invoked, they’ll try to access the list with an index that’s equal to its count, thus off by one = argument out of range.

Simple fix: use a local variable that is declared within the loop’s scope. Each new listener will capture its very own version of that variable without any other code altering the value of the captured variable.
I know it may look ugly, but that’s how you capture variables properly in a loop.

2 Likes

Oh ok, thank you very much.