- Home /
The question is answered, right answer was accepted
Index out of bounds even tho it shouldnt be [Javascript]
Hello! I have a problem with part of my programm. I was trying to save instances of a GameObject into an array for later which worked just fine. Now i wanted to iterate though the array with a "for" loop. Going from slot 0 to slot array.length.
.
For some reason this gives me an error message which is this one:
ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count. Parameter name: index 1 System.Collections.ArrayList.ThrowNewArgumentOutOfRangeException (System.String name, System.Object actual, System.String message) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections/ArrayList.cs:3261) System.Collections.ArrayList.get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections/ArrayList.cs:2652) UnityScript.Lang.Array.get_Item (Int32 index) ButtonFucntions.SaveValues () (at Assets/Scripts/ButtonFucntions.js:177) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()
.
My Code:
for(var i : int = 0; i <= eventsArray.length; i++){
DayDisplayInstance = eventsArray[i];
child = DayDisplayInstance.transform.GetChild(0).gameObject;
myText = child.GetComponent.<Text>();
print(myText.text);
}
I don't know if this is the easiest solution to get the Text component of a child of my Object but it works.
Any ideas what my problem could be? It should be pretty simple I guess, I have just never really worked with arrays in Unity.
.
Thanks a lot in advance!!!
(if you need more parts of the code feel free to ask)
actually i haven not! Let me try that realy quickly.
It prints: 0 then myText.text then 1 (there isnt a new object on slot 1 of the array yet) then the out of bounds hits (im assu$$anonymous$$g thats when it tries to acces the object on slot 1 which doesnt exist). But why would it do that? After all i am saying it should only iterate till array.length? $$anonymous$$aybe array.length-1? I will try that now.
Ok i am dumb. Ofc it didnt work. The index is length-1. That was my problem since arrays start with index 0. Thanks a bunch for re$$anonymous$$ding me of that in a way!
Answer by Remy_Unity · Jan 30, 2018 at 12:24 PM
The error is here :
i <= eventsArray.length
At the last iteration of your loop, i will be equal to the array length, and this is not a valid index. The last valid index is array.length-1. For a 10 items array, index goes from 0 to 9.
Just replace "
Thanks for your help! I just realised.. It's alsways the tiny mistakes for me.
Follow this Question
Related Questions
How To Get A Reference To All Nearby GameObjects? 1 Answer
Instantiate from array into array? 2 Answers
How to set parents of all objects in an array? 1 Answer
Transform array help 1 Answer
for loop error 2 Answers