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 PurpleComet · Sep 28, 2015 at 08:49 PM · arraysstartexceptioninvoke

IndexOutOfRangeException for initial spawn of prefabs from an array

I've been following along with a tutorial on Infinite Runners found here:

https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/infinite-runner

I'm doing a slightly modified version for my own project. Everything seems to work fine, except the game gives me an IndexOutOfRangeException immediately after I press the play button. Afterwards, the Spawn function seems to have no problems. Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class SpawnScript : MonoBehaviour {
 
     public GameObject[] obj;
     public float spawnMin = 3f;
     public float spawnMax = 4f;
 
     // Use this for initialization
     void Start () {
         Spawn ();
     }
 
     void Spawn () {
         int random = Random.Range(0, obj.Length);
         Debug.Log("Random array number: " + random + " GetLength: " + obj.GetLength(0) + " Length: " + obj.Length);
         Instantiate(obj[random], transform.position, Quaternion.identity);
         Invoke ("Spawn", Random.Range(spawnMin, spawnMax));
     }
 }

Here is a screencap of the quad this script is attached to:

alt text (The quad is a child of the Main Camera object)

As you can see I added a logging statement, here's the console output, including the logging statement and error:

Random array number: 1 GetLength: 2 Length: 2

UnityEngine.Debug:Log(Object)

SpawnScript:Spawn() (at Assets/Scripts/SpawnScript.cs:29)

SpawnScript:Start() (atAssets/Scripts/SpawnScript.cs:14)

Random array number: 0 GetLength: 0 Length: 0

UnityEngine.Debug:Log(Object) SpawnScript:Spawn() (at Assets/Scripts/SpawnScript.cs:29)

SpawnScript:Start() (at Assets/Scripts/SpawnScript.cs:14)

IndexOutOfRangeException: Array index is out of range.

SpawnScript.Spawn () (at Assets/Scripts/SpawnScript.cs:30)

SpawnScript.Start () (at Assets/Scripts/SpawnScript.cs:14)

Random array number: 1 GetLength: 2 Length: 2

UnityEngine.Debug:Log(Object) SpawnScript:Spawn() (at Assets/Scripts/SpawnScript.cs:29)

Strangely enough, it seems that Start function calls Spawn() twice. The first time it shows the correct length of the array, but the second it shows an empty array. Any idea why this is happening?

screen-shot-2015-09-28-at-40647-pm.png (28.9 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Cherno · Sep 28, 2015 at 09:00 PM

  1. You use GetLength with a one-dimensional array. GetLength is only used for multi-dimensional arrays where GetLength(3] would return the Length of the fourth dimension of the array, for example. For one-dimensional arrays, only use Length.

  2. Maybe you have a second Spawn component on some GameObject in the scene? Also, it would help to know which exact line of code the error occurs in, since the message your posted says line 30 which is beyond the code you posted.

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 PurpleComet · Sep 28, 2015 at 10:45 PM 0
Share
  1. Ok, I would've just used Length but for some reason the guy in the tutorial used GetLength and seemed to say that using Length was incorrect.

  2. You were right. Somehow the SpawnScript was also on the $$anonymous$$ain Camera object. Thanks for your help!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

For loop not running correctly inside start () 1 Answer

How can I get every other GameObject except for the one I'm using? 1 Answer

Invoke fails: "Trying to Invoke method: InputController.enableInput couldn't be called." 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Invoke not working from Start() ? 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