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 /
  • Help Room /
avatar image
0
Question by Hpgames45 · Mar 03, 2016 at 03:24 PM · c#errorarrayarrays

Index out of range error

Here is my code

using UnityEngine; using System.Collections;

public class SpawnScript : MonoBehaviour {

 public GameObject[] objects;
 public float spawnMin = 1f;
 public float spawnMax = 2f;

 // Use this for initialization
 void Start () {
     Spawn();

 
 }

 void Spawn()
 {
     Instantiate(objects[Random.Range(0, objects.GetLength(0))],
         new Vector3(transform.position.x + 20, transform.position.y, 0) ,
         Quaternion.identity);
     Invoke("Spawn", Random.Range(spawnMin, spawnMax));
 }

}

but I am getting this error.

IndexOutOfRangeException: Array index is out of range. SpawnScript.Spawn () (at Assets/Script/SpawnScript.cs:19) SpawnScript.Start () (at Assets/Script/SpawnScript.cs:12)

any ideas to what I am doing wrong?

Comment
Add comment · Show 7
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 gjf · Mar 03, 2016 at 02:08 PM 0
Share

objects.GetLength(0) probably isn't returning the length of the objects array, wouldn't that be something like objects.Length?

avatar image Hpgames45 gjf · Mar 03, 2016 at 02:30 PM 0
Share

I already tried objects.Length, same error

avatar image Salmjak · Mar 03, 2016 at 05:29 PM 0
Share
 objects.GetLength(0)

This will return the size of the array. The last position will always be the size-1 (since the first position is 0). So try changing that to objects.GetLength(0)-1.

EDIT: Although, Random.Range(int,int) should be exclusive at the max int... (so you should get maximum objects.GetLength(0)-1 already).

avatar image saschandroid · Mar 07, 2016 at 07:22 AM 0
Share

You should check the length of your array before calling Spawn(). With no elements in the array (length = 0), Random.Range(0, 0) will return 0 and then you would get an IndexOutOfRangeException because objects[0] does not exist.

avatar image Ali-hatem · Mar 07, 2016 at 11:38 AM 0
Share

@Hpgames45

  1. have you added the objects to this script in the inspector.

  2. have you modfied the spawn$$anonymous$$in & spawn$$anonymous$$ax in the inspector .

avatar image Hpgames45 Ali-hatem · Mar 07, 2016 at 01:15 PM 0
Share

I have added the objects (and the side) in the inspector, and I modified spawn$$anonymous$$in & spawn$$anonymous$$ax in the inspector aswell.

avatar image Statement Hpgames45 · Mar 07, 2016 at 05:02 PM 0
Share

Are you sure you don't have any other scripts in the scene where you forgot to add objects to the array? The only reason your code as listed would fail, would be if the array was empty because accessing index 0 in an empty array would throw that error. In all other cases, Random.Range will work when used in this manner.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Mar 07, 2016 at 05:06 PM

Try adding a guard to see if your array is empty or not. If the message pops up in the console, single click the message to see which game object the script sits on, and ensure you got objects in the array.

 using UnityEngine;
 using System.Collections;
 
 public class SpawnScript : MonoBehaviour {
 
     public GameObject[] objects;
     public float spawnMin = 1f;
     public float spawnMax = 2f;
 
     // Use this for initialization
     void Start () {
         Spawn();
     }
 
     void Spawn()
     {
         if (objects.GetLength(0) == 0)
         {
             print("No objects in array!");                 
             Invoke("Spawn", Random.Range(spawnMin, spawnMax));
             return;
         }
 
         Instantiate(objects[Random.Range(0, objects.GetLength(0))],
             new Vector3(transform.position.x + 20, transform.position.y, 0) ,
             Quaternion.identity);
         Invoke("Spawn", Random.Range(spawnMin, spawnMax));
     }
 }
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 Ali-hatem · Mar 07, 2016 at 05:56 PM 0
Share

for some reason when i first tested his code in unity i got the same problem but now i re tested the same code & the problem is gone so i will delete my answer if it bathers you . by the way i have already asked him if the array is empty & he said now

have you added the objects to this script in the inspector.

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

9 People are following this question.

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

Related Questions

Null reference exception in an if statement that checks for it. 1 Answer

Convert string to array name? 0 Answers

How to get all children of a Gameobject with a certain component 2 Answers

Cant define array from other script 0 Answers

Can't access public static array from another class 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