Spawning an object at a random time | C#

I’m creating a C# script that counts to a random number, once its done counting it will spawn an object.

The problem is, sometimes the spawning is off and the objects get spawned on top of one another.

The code:
using UnityEngine;
using System.Collections;

public class RandomTimeSpawner : MonoBehaviour {

	//Spawn this object
	public GameObject spawnObject;

	public float maxTime = 5;
	public float minTime = 2;

	//current time
	private float time;

	//The time to spawn the object
	private float spawnTime;

	void Start(){
		SetRandomTime();
		time = minTime;
	}

	void FixedUpdate(){

		//Counts up
		time += Time.deltaTime;

		//Check if its the right time to spawn the object
		if(time >= spawnTime){
			SpawnObject();
			SetRandomTime();
		}

	}


	//Spawns the object and resets the time
	void SpawnObject(){
		time = minTime;
		Instantiate (spawnObject, transform.position, spawnObject.transform.rotation);
	}

	//Sets the random time between minTime and maxTime
	void SetRandomTime(){
		spawnTime = Random.Range(minTime, maxTime);
	}

}

Any help would be greatly appreciated! :smiley:

void SpawnObject(){
time = 0; //Change this line
Instantiate (spawnObject, transform.position, spawnObject.transform.rotation);
}

This is my version:

using UnityEngine;

using System.Collections.Generic;


public class EnemySpawner : MonoBehaviour
{
	// The enemy prefab to be spawned.
	public GameObject enemy;

	// An array of the spawn points this enemy can spawn from.
	private Transform[] spawnPoints;

	public float maxTime = 50;

	public float minTime = 10;

	//current time
	private float time;
 
	//The time to spawn the object
	private float spawnTime;


	void Awake ()
	{

		List<Transform> spawningPointsAsList = new List<Transform> ();

		// Get All the children of the object this script is assigned to (EnemyManager) and consider them as spawining points
		foreach (Transform child in transform) {
			spawningPointsAsList.Add (child);
		}

		spawnPoints = spawningPointsAsList.ToArray ();
	}


	void Start ()
	{
		SetRandomTime ();
		time = 0;
	}

	//Sets the random time between minTime and maxTime
	void SetRandomTime ()
	{
		spawnTime = Random.Range (minTime, maxTime);
		Debug.Log ("Next object spawn in " + spawnTime + " seconds.");
	}

	void FixedUpdate ()
	{
		//Counts up
		time += Time.deltaTime;
		//Check if its the right time to spawn the object
		if (time >= spawnTime) {
			Debug.Log ("Time to spawn: " + enemy.name);
			Spawn ();
			SetRandomTime ();
			time = 0;
		}
	}


	void Spawn ()
	{
		// Find a random index between zero and one less than the number of spawn points.
		int spawnPointIndex = Random.Range (0, spawnPoints.Length);
		// Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
		Instantiate (enemy, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
	}
}