Spawn Random Object script spawns too much

Hey!
So i have this Spawn Script which should spwan objects in a specific area each X seconds.

using UnityEngine;
using System.Collections;

public class SpawnGameObjects : 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 ()
	{
		Vector3 spawnPosition;

		// get a random position between the specified ranges
		spawnPosition.x = Random.Range (xMinRange, xMaxRange);
		spawnPosition.y = Random.Range (yMinRange, yMaxRange);
		spawnPosition.z = Random.Range (zMinRange, zMaxRange);
		if ((spawnPosition.z < 4 && spawnPosition.z > -4) || (spawnPosition.x < 4 && spawnPosition.x > -4)) 
		{
			MakeThingToSpawn ();
		}

		// 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;
	}
}

but there is a weird bug which leads to the behavior that somethimes 3 objects get spawned, sometimes 5, sometimes 1… but it should only spawn 1 object. can someone spot the mistake?

1 Answer

1

Inside MakeThingToSpawn, where you call MakeThingToSpawn again, you are making the function call recursive - meaning multiple calls are happening simultaneously depending on how the random numbers play out:

MakeThingToSpawn

if (numbers in a certain range)

—MakeThingToSpawn

—if (number in a certain range)

------MakeThingToSpawn

… and so on

I’m guessing this is what you’re looking for. Rather then calling the method over and over again when the position is incorrect, this just recalulates the value until it is right.

     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;
     }

perfect, thank you :)