- Home /
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! :D
Don't you want to be resetting time to 0 in SpawnObject, rather than to $$anonymous$$Time? As things stand you're spawning at an interval that's randomised between 0 and (maxTime-$$anonymous$$Time), rather than between $$anonymous$$Time and maxTime which I'm assu$$anonymous$$g is what's intended.
Why "time = $$anonymous$$Time"? I guess u need "time = 0" at SpawnObject() function. If your time always equals $$anonymous$$Time your spawner can get "spawnTime" with same number, and thats why you have multiple spawns.
Thank you very much, that didn't even cross my $$anonymous$$d! :D
Answer by jenci1990 · Feb 11, 2015 at 03:25 PM
void SpawnObject(){
time = 0; //Change this line
Instantiate (spawnObject, transform.position, spawnObject.transform.rotation);
}
I am using this (great) script, but am having a problem. I can see the clones as they are generated in the Hierarchy, but certain prefabs are not visible in the game. Any idea what is causing this? I have tried to space out my empty objects as much as possible.
@Zeesy There are loads of possibilities. I'd probably start by deactivating them one at a time (in the inspector) to identify which ones aren't visible. Then focus on those: are they within sight of the camera? (you can see their positions in the inspector and also look at the scene view) If you're not sure you can just use the inspector to move them to the same place as one of the visible ones, are they now visible? If not, are they in the right layer? Check the renderer/material, and so on. Or is it just that some have been spawned at the same place as each other?
By the way, I am assu$$anonymous$$g that you meant to say "...but certain clones are not visible in the game".
Thanks, @Bonfire-Boy. I moved the empty objects around, and tightened up the prefabs' colliders, and everything seems to be working fine now. Next challenge is to have them spawn at random locations...
Answer by Dani-Desa · Feb 03, 2018 at 11:40 PM
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);
}
}
Your answer
Follow this Question
Related Questions
c# Enemy's spawning over time, problem 2 Answers
C# 2d Instantiate object randomly around gameobject 1 Answer
Randomize Spawn Times 0 Answers
Trying to delay movement of a GameObject 1 Answer
How to change the colour of a light every few seconds. 1 Answer