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 importguru88 · Aug 16, 2016 at 11:23 PM · arrayexceptionindexout of range

IndexOutOfRangeException: Array index is out of range / EnemiesSpawner.cs:18)

I am getting this error IndexOutOfRangeException: Array index is out of range. EnemiesSpawner.Spawn () (at Assets/EnemiesSpawner.cs:18)

It keeps popping up . When do I spawn my enemies they spawn out fine to me . Here is my script :

 using UnityEngine;
 using System.Collections;
 
 public class EnemiesSpawner : MonoBehaviour {
 public GameObject enemy;
 public Transform [] spawnPoints;
 public float spawnTime = 5f;
     
     void Start () {
      InvokeRepeating("Spawn", spawnTime, spawnTime); //Calls the "Spawn" function every 10 seconds.
 
     }
     
 
     void Spawn () {
     
  int spawnPointIndex = Random.Range (0, spawnPoints.Length); for( int spawnCount = 20 ; spawnCount > 0 ; --spawnCount )
                  Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    
 
     
     }
 }
 

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

3 Replies

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

Answer by nate-sewell · Aug 17, 2016 at 03:48 AM

personally I'd use: int spawnCount = spawnPoints.Length - 1

rather than: int spawnCount = 20

also, make sure that spawnPoints has actually been initialized AND populated based on the fact that it's public in a MonoBehaviour, it is /possible/ that it's initialized in the editor...

also spawnCount > 0 means the initial entry will never be used

Comment
Add comment · Show 20 · 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 importguru88 · Aug 17, 2016 at 03:59 AM 0
Share

When I deleted the old code and add the int spawnCount = spawnPoints.Length - 1 it wouldn't spawn any enemies. I trying to spawn to different enemies and one spawn the other one didn't why is that ?

avatar image importguru88 · Aug 17, 2016 at 04:00 AM 0
Share

How would I spawn 20 enemy ai's at one time ?

avatar image nate-sewell · Aug 17, 2016 at 04:06 AM 0
Share

if you have 2 you want to use the indices 0, 1

using spawnPoints.Length - 1 gets you the correct top number

using spawnCount > 0 prevents you from entering the loop when you get down to 0

to get the full range range use this as the loop condition spawnCount >= 0

avatar image importguru88 nate-sewell · Aug 17, 2016 at 04:12 AM 0
Share

What do mean indices 0,1 . ? I am not understanding you . What would be an example of 20 or less in code .

The is what I have for for now :

 using UnityEngine;
 using System.Collections;
 
 public class EnemiesSpawner : $$anonymous$$onoBehaviour {
 public GameObject enemy;
 public Transform [] spawnPoints;
 public float spawnTime = 5f;
     
     void Start () {
      InvokeRepeating("Spawn", spawnTime, spawnTime); //Calls the "Spawn" function every 10 seconds.
 
     }
     
 
     void Spawn () {
     
  int spawnPointIndex = Random.Range (0, spawnPoints.Length); for( int spawnCount = spawnPoints.Length - 1 ; spawnCount >= 0 ; --spawnCount )
                  Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    
 
     
     }
 }
 
avatar image nate-sewell importguru88 · Aug 17, 2016 at 04:18 AM 0
Share

I think perhaps the other poster had a more accurate answer than the one I gave, for some reason I thought you were using the spawnCount variable inside the for loop's body as well, but it lloks like you used spawnPointIndex ins$$anonymous$$d.... anyway, it should look something like this

    int spawnPointIndex = Random.Range (0, spawnPoints.Length -1);
    for( int spawnCount = 20 - 1 ; spawnCount >= 0 ; --spawnCount )
                      Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);

Show more comments
avatar image importguru88 nate-sewell · Aug 17, 2016 at 05:12 AM 0
Share

Yes I do...

avatar image nate-sewell importguru88 · Aug 17, 2016 at 05:15 AM 0
Share

can you screen cap the portion of the editor where you assigned values into the SpawnPoints array and attach it in a response?

avatar image importguru88 nate-sewell · Aug 17, 2016 at 05:16 AM 0
Share

Do you mean editor setting ?

avatar image nate-sewell importguru88 · Aug 17, 2016 at 05:18 AM 0
Share

yup, the section where you attached the script to something in the scene or a prefab

Show more comments
Show more comments
avatar image
-1

Answer by Cherno · Aug 17, 2016 at 03:43 AM

Using Random.Range with ints makes the max value be inclusive; that means that if you have an array length of 3 (elements at index 0,1,2) then with your code you get a value of 0,1,2 or 3. Since 3 is higher than the highest index value (2) you get the out of range error.

Solution: Use spawnPoints.Length - 1 for the max value.

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 tanoshimi · Aug 17, 2016 at 06:02 AM 0
Share

This is incorrect. "Note that max is exclusive, so using Random.Range( 0, 10 ) will return values between 0 and 9" - https://docs.unity3d.com/ScriptReference/Random.Range.html

avatar image
0

Answer by tanoshimi · Aug 17, 2016 at 06:16 AM

From the look of your screenshot, the object which you're spawning is the same as the object to which this script is attached? That sounds wrong, and will lead to an exponential growth in the number of spawners (the new clones of which won't have any spawnobjects assigned).... I would suggest that you have a single spawner manager object to wbich this script is attached, but it should not also be attached to individual spawnpoints.

If that does not resolve your index out of range, make liberal use of Debug.Logs to print values of each of your variables.

Comment
Add comment · Show 7 · 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 importguru88 · Aug 17, 2016 at 06:21 AM 0
Share

Okay I want to spawn in different location in scene .

avatar image nate-sewell · Aug 17, 2016 at 06:22 AM 0
Share

I thought that when I saw the screenshot, but it is actually a spawnPoint that is self referential, not the item being spawned I recreated the scenario as best I could but ti works fine for me.

avatar image importguru88 · Aug 17, 2016 at 06:22 AM 0
Share

How would I spawn two different prefabs ?

avatar image importguru88 · Aug 17, 2016 at 06:22 AM 0
Share

on one spawn point

avatar image nate-sewell importguru88 · Aug 17, 2016 at 06:27 AM 0
Share

if you are wanting to spawn two different prefabs at the same spot...

Reuse the script and for enemy use a different object from the scene or prefab, and fill in the spawn points the same way you have it now

avatar image importguru88 nate-sewell · Aug 17, 2016 at 07:49 AM 0
Share

That how I got it set up .

Show more comments

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

60 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

Related Questions

Array Index Is Out Of Range... 1 Answer

,"For" loop looping only once through my array 0 Answers

Unknown Argument Out of Range Index Error On Card Game 1 Answer

index out of range problem with for loops and arrays. 1 Answer

Reference all objects in an Array except for the object the script is on. 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