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 Shkizi · Oct 06, 2015 at 04:18 PM · arrayspawn

Spawn array error

Hello Im trying to spawn obstacles on my game but im having an array error i can't understand

Im new to unit. So i already created obstacles and put it on my prefabs folder.

error: IndexOutOfRangeException: Array index is out of range. Spawner+c__Iterator0.MoveNext () (at Assets/Scripts/Spawner.cs:25)

Code:

 using UnityEngine;
 using System.Collections;
 
 public class Spawner : MonoBehaviour 
 
 {    
     public GameObject [] prefabs;
     public float delay = 2.0f; // Delay on spawn
     public bool active = true; // Define to cancel the spawn when the game is over
     // Use this for initialization
     void Start () 
     {
         StartCoroutine (ObstacleGenerator ());
 
     }
 
     IEnumerator ObstacleGenerator() 
     {
         yield return new WaitForSeconds (delay);
 
         if (active) 
         {
             var newTransform = transform; 
 
             Instantiate(prefabs[Random.Range(0, prefabs.Length)], newTransform.position, Quaternion.identity); 
         }
 
         StartCoroutine (ObstacleGenerator ());
     }
 }

Thank you

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Gilles_aerts · Oct 06, 2015 at 05:44 PM

If the index is out of range it simply means that the script tries to acces a part of the array that doesn't exists. e.g : if you ask for the fifth element of an array but there are only 4 elements in it.. then the index is out of range because it doesn't know what the 5th element is since it doesn't exists.

If I look at you code it should never be out of range since you only ask to acces randomly between 0 an however long the array may be...

The only thing I can think of is that in your inspector maybe one of the elements was left empty causing the out of range.

I may have not solved you problems and my apologies for that. But now you know what it means when your index is out of range.

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 Shkizi · Oct 06, 2015 at 10:19 PM

I have 3 obstacles on prefab folder. The error that shows is directed to the line of the Instantiate. I can't figured why

But thank you for your help!

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

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

31 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

Related Questions

Spawning limited GameObjects at a specific position not working 1 Answer

spawning problems, using array for random objects,Spawning rand prefab from array, destroy when not in the view of camera, roll new one. 0 Answers

How to Spawn a Prefab at an already Spawned Prefab's that's from an array at their transform.position? 1 Answer

Move enemy to a specific movepoint with an array (C#) 1 Answer

How to make an array that stores x, y and z co-ordinates? 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