0

This is my spawning script belowwritten in c# The script should create objects randomly in the scene.

The issue is that I'm getting this error at runtime.

IndexOutOfRangeException: Array index is out of range.
CreateEasterEggs.MakeThingToSpawn () (at Assets/CreateEasterEggs.cs:52)
CreateEasterEggs.Update () (at Assets/CreateEasterEggs.cs:28)

Not sure what I have done wrong, thinking its something to do with the game object?

Thank you.


using UnityEngine;
 using System.Collections;

 public class CreateEasterEggs : MonoBehaviour
 {
     public float secondsBetweenSpawning = 0.1f;
     public float xMinRange = -25.0f;
     public float xMaxRange = 25.0f;
     public float yMinRange = -5.0f;
     public float yMaxRange = 0.0f;
     public float zMinRange = -25.0f;
     public float zMaxRange = 25.0f;
     public GameObject[] spawnObjects; // what prefabs to spawn

     private float nextSpawnTime;

     void Start ()
     {
         // determine when to spawn the next object
         nextSpawnTime = Time.time+secondsBetweenSpawning;
     }

     void Update ()
     {
         // if time to spawn a new game object
         if (Time.time  >= nextSpawnTime) {
             // Spawn the game object through function below
             MakeThingToSpawn ();

             // determine the next time to spawn the object
             nextSpawnTime = Time.time+secondsBetweenSpawning;
         }   
     }

    void MakeThingToSpawn ()
      {
          //Start the vector at an invalid position
          Vector3 spawnPosition = new Vector3(0, 0, 0);

          //while we are not in the right range, continually regenerate the position
          while ((spawnPosition.z < 4 && spawnPosition.z > -4) || (spawnPosition.x < 4 && spawnPosition.x > -4)) 
          {
              spawnPosition.x = Random.Range (xMinRange, xMaxRange);
              spawnPosition.y = Random.Range (yMinRange, yMaxRange);
              spawnPosition.z = Random.Range (zMinRange, zMaxRange);
          }

          // determine which object to spawn
          int objectToSpawn = Random.Range (0, spawnObjects.Length);

          // actually spawn the game object
              GameObject spawnedObject = Instantiate (spawnObjects [objectToSpawn], spawnPosition, transform.rotation) as GameObject;

          // make the parent the spawner so hierarchy doesn't get super messy
          spawnedObject.transform.parent = gameObject.transform;
      }
 }
2
  • 1
    The only thing I can see that can cause it is if the spawnObjects array is empty. Have you added GameObjects to the array in the inspector? Else you can print objectToSpawn and see what is prints and go from there. Commented Feb 26, 2017 at 9:29
  • @JohanLindkvist ;-/ opps yes that was it, thank you! Make it the answer and I'll accept. Commented Feb 26, 2017 at 10:55

1 Answer 1

2

IndexOutOfRange means that you tried to access to an element of an array that doesn't exist.

In your case as you are doing it with Random.Range (0, spawnObjects.Length); Then the only possible case is that your array is empty.

Try to Debug.Log(spawnObjects.Length): before the Instantiate and you will see that in fact your array of gameobjects is empty as It will return 0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.