Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jan 30, 2018 at 12:27 PM by DonkeyMalonkey for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DonkeyMalonkey · Jan 30, 2018 at 11:57 AM · javascriptgameobjectarrayarrays

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)

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dev-waqas · Jan 30, 2018 at 12:15 PM 1
Share

Have you tried printing the value of i ?

avatar image DonkeyMalonkey dev-waqas · Jan 30, 2018 at 12:19 PM 0
Share

actually i haven not! Let me try that realy quickly.

avatar image DonkeyMalonkey dev-waqas · Jan 30, 2018 at 12:23 PM 0
Share

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.

avatar image DonkeyMalonkey dev-waqas · Jan 30, 2018 at 12:27 PM 0
Share

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!

1 Reply

  • Sort: 
avatar image
1
Best Answer

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 "

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DonkeyMalonkey · Jan 30, 2018 at 12:29 PM 0
Share

Thanks for your help! I just realised.. It's alsways the tiny mistakes for me.

Follow this Question

Answers Answers and Comments

156 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges