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 /
avatar image
0
Question by DatTardisDoh · Apr 25, 2018 at 10:22 PM · errorarrayerror messageout of range

IndexOutOfRangeException: Array index is out of range

I have a GameObject[] called walkways and it's giving me an "Array index is out of range" error code despite the fact that I have the call maximum as "walkways.Length."

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class spawnwalkway : MonoBehaviour {
     public GameObject[] walkways;
     public int plat_num_random;
     // Use this for initialization
     void Start () {
         StartCoroutine(spawnBlock());
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     IEnumerator spawnBlock()
     {
         walkways = GameObject.FindGameObjectsWithTag("walkway");
         for (int z = 1; z < 100; z++)
         {
             Instantiate(walkways[Random.Range(0,walkways.Length)], new Vector3(-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
             yield return new WaitForSeconds(0.5f);
         }
 
     }
 }

Comment
Add comment · Show 1
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 bobisgod234 · Apr 25, 2018 at 10:54 PM 0
Share

Debug.Log() the length of the walkways array after the "FindGameObjectsWithTag" call. You could be getting that error because walkways is empty.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Hellium · Apr 26, 2018 at 07:08 AM

Even though you use walkways.Length when calling Random.Range, there is a case you haven't handled: What if walkways does not contain any element? Then, Random.Range would return 0. However, walkways[0] does not exist since the array is empty.


So, make sure walkways has element before calling your function.

      walkways = GameObject.FindGameObjectsWithTag("walkway");
      if( walkways.Length > 0 )
      {
          for (int z = 1; z < 100; z++)
          {
              Instantiate(walkways[Random.Range(0, walkways.Length)], new Vector3(-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
              yield return new WaitForSeconds(0.5f);
          }
      }
      else
          Debug.LogError("No walkway found! Are you sure the objects are correctly tagged?");
Comment
Add comment · 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
0

Answer by KittenSnipes · Apr 26, 2018 at 02:49 AM

@DatTardisDoh

I think that was the problem.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class spawnwalkway : MonoBehaviour
 {
     public GameObject[] walkways;
     public int plat_num_random;
     // Use this for initialization
     void Start()
     {
         StartCoroutine(spawnBlock());
     }
 
     IEnumerator spawnBlock()
     {
         walkways = GameObject.FindGameObjectsWithTag("walkway");
         for (int z = 1; z < 100; z++)
         {
             //Changed from walkways[Random.Range(0, walkways.Length)]
             //to walkways[Random.Range(0, walkways.Length - 1)]
             Instantiate(walkways[Random.Range(0, walkways.Length - 1)], new Vector3(-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
             yield return new WaitForSeconds(0.5f);
         }
 
     }
 }
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 Hellium · Apr 26, 2018 at 07:05 AM -1
Share

public static int Range(int $$anonymous$$, int max);
Description
Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive] (Read Only).


So no need for the walkways.Length - 1

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 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

Related Questions

IndexOutOfRangeException help on debug 3 Answers

Array index is out of range Error, only when array is used to Instantiate 4 Answers

Cant fetch components from GameObjects stored in an array? 0 Answers

Array Out of Index (When it Really is not??) 1 Answer

Too many errors in the IDE (even for a fresh project), but editor doesn't throw any errors! 1 Answer


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