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 AgarFun · Jul 03, 2017 at 01:35 AM · spawnrandom.rangeintinvokeinvokerepeating

IndexOutOfRangeException: Array index is out of range

IndexOutOfRangeException: Array index is out of range, it's my problem with this script at 37th line (GameObject Newaster = Instantiate (aster[spawnObject], spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation) as GameObject;):

using System.Collections; using UnityEngine; using UnityEngine.Networking; using System.Collections.Generic;

public class Asteroid : NetworkBehaviour {

 public float timeTospawn = 0.5f;
 public Transform[] spawnPoints;
 public GameObject[] aster;

 public List<Transform> possibleSpawns = new List<Transform>();


 // Use this for initialization
 void Start()
 {
     //fill possible spawn
     for (int i = 0;i<spawnPoints.Length;i++)
     {
         possibleSpawns.Add(spawnPoints[i]);
     }
     InvokeRepeating("SpawnItems", timeTospawn, timeTospawn);
 }

 // Update is called once per frame
 void Update () {
     
 }
  
 
 void SpawnItems()
 {
     int spawnIndex = Random.Range(0, possibleSpawns.Count);
     int spawnObject = Random.Range(0, aster.Length);
     GameObject Newaster =  Instantiate (aster[spawnObject], spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation) as GameObject;
     Newaster.GetComponent<DestroyAsteroid>().mySpawnPoint = possibleSpawns[spawnIndex];

     possibleSpawns.RemoveAt(spawnIndex);
 }

}

Comment
Add comment · Show 4
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 BaronScreenKiller · Jul 03, 2017 at 04:49 AM 0
Share

So the issue is that the array element you're trying to access doesn't exist.

One possible issue: Does it happen time its called or does it work sometimes? Random.Range is inclusive which means it can return the highest possible value. Say your array has 10 elements, so aster.Length = 10. that means spawnObject has a chance to equal 10. but the 10th element in the array is element 9, because it counts from 0, not 1. I'd try:

int spawnObject = Random.Range(0, aster.Length - 1);

The list may have a similar issue.

Alternatively:

You're using possibleSpawns.Count to set spawnIndex, and then using spawnIndex to access an element from spawnPoints.

Is spawnPoints definitely longer than possibleSpawns?

I'd be debug.logging all the array lengths and index values and making sure they are within each others ranges. Sorry I can't help more but it kinda depends on what's in your public arrays

avatar image AgarFun · Jul 03, 2017 at 05:57 AM 0
Share

Thank's to reply, but it don't works, now it's this line who doesnt works with the same error : GameObject Newaster = Instantiate (aster[spawnObject], spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation) as GameObject;

Can you send me all the line changed

avatar image AgarFun AgarFun · Jul 03, 2017 at 05:58 AM 0
Share

Because I'm not English and I didn't understand all you said

avatar image BaronScreenKiller AgarFun · Jul 03, 2017 at 06:00 AM 0
Share

There is nothing necessarily wrong with than line. the issue may be the length of your public arrays. I suggest debug.logging out the length of those arrays and the indexes to make sure they are correct before the line it errors on.

keeping in $$anonymous$$d that the 10th element in an array is element 9 (it will always be one less)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AgarFun · Jul 03, 2017 at 05:56 AM

Thank's to reply, but it don't works, now it's this line who doesnt works with the same error : GameObject Newaster = Instantiate (aster[spawnObject], spawnPoints [spawnIndex].position, spawnPoints [spawnIndex].rotation) as GameObject;

Can you send me all the line changed

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

66 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

Related Questions

Ontriggerenter and InvokeRepeating problem 1 Answer

How To Random Range a Float? 2 Answers

How to spawn prefab from a single cache when using multiple caches? 0 Answers

I need to Stop and Restart a Co-Routine at anytime 1 Answer

Spawn Controller... 2 Answers


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