Well Im not a especially good unity Dev but I would have done something like this:
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SpawnPrefabController.cs" company="GoDialog">
// Copyright (C) 2016 Jonas Fjeld
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program. If not, see <http://www.gnu.org/licenses/>
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections.Generic;
using Random = UnityEngine.Random;
public class SpawnPrefabController : MonoBehaviour
{
/// <summary>
/// GameObject to Spawn
/// </summary>
public GameObject SpawnItem;
/// <summary>
/// Is the SpawnController active?
/// </summary>
public bool ControllerActive = false;
/// <summary>
/// List of the spawned GameObjects
/// </summary>
public List<GameObject> SpawnedObjects;
/// <summary>
/// How many objects to spawn at one time?
/// </summary>
public int SpawnAtOnce = 3;
/// <summary>
/// How many objects should be spawned
/// </summary>
public int MaxSpawnedObjects = 10;
/// <summary>
/// Whats the delay between each spawn group?
/// </summary>
public float SpawnEatch = 2;
/// <summary>
/// Spawn the full SpawnAtOnce group or just to the spawn cap?
/// </summary>
public bool OverSpawn = false;
/// <summary>
/// The time left to next spawn group
/// </summary>
private float spawnTimeLeft;
/// <summary>
/// Gets a random Possition to spawn the object
/// </summary>
private Vector3 spawnPosoition
{
get
{
return new Vector3(
Random.Range(0f, 10f),
Random.Range(0f, 10f),
Random.Range(0f, 10f));
}
}
/// <summary>
/// Script initialization
/// </summary>
void Start ()
{
// initialize List
SpawnedObjects = new List<GameObject>(MaxSpawnedObjects);
// Set the spawnTime left so we have a plase to start counting down from
// This could be 0f to start spawning right away.
spawnTimeLeft = SpawnEatch;
}
/// <summary>
/// Update is called once per frame
/// </summary>
void FixedUpdate()
{
// Check if Objects has been deleted from array
for (var i = 0; i < this.SpawnedObjects.Count; i++)
{
if (SpawnedObjects *== null)*
{
this.SpawnedObjects.RemoveAt(i);
}
}
// Is the controller active?
if (ControllerActive)
{
// Is it time to do a new batch of spawning
if (spawnTimeLeft <= 0)
{
// Is there room for more objects?
if (SpawnedObjects.Count < MaxSpawnedObjects)
{
// Should we Over spawn
if (OverSpawn)
{
// Debug.Log(“Over Spawning " + SpawnAtOnce + " Objects”);
for (int i = 0; i < SpawnAtOnce; i++)
{
// Instantiate Object
var o = (GameObject)Instantiate(SpawnItem, spawnPosoition, Quaternion.identity);
// Set its parrent to this controller
o.transform.parent = this.transform;
// Add Spawned Objects to List
SpawnedObjects.Add(o);
}
}
else
{
var itemsAllowedToSpawn = MaxSpawnedObjects - SpawnedObjects.Count ;
var itemsToSpawn = Mathf.Clamp(itemsAllowedToSpawn,0,SpawnAtOnce);
//Debug.Log(“Spawning " + itemsToSpawn + " Objects”);
for (int i = 0; i < itemsToSpawn; i++)
{
// Instantiate Object
var o = (GameObject)Instantiate(SpawnItem, spawnPosoition, Quaternion.identity);
// Set its parrent to this controller
o.transform.parent = this.transform;
// Add Spawned Objects to List
SpawnedObjects.Add(o);
}
}
}
// We are done for this time so we reset the spawn timer
spawnTimeLeft = SpawnEatch;
// Debug.Log(“DoneSpawning”);
}
// We have to tick down the spawn timer
spawnTimeLeft -= Time.deltaTime;
// Debug.Log(“tick”);
}
}
}
EDIT: Wrote it for 3D just of habit, but just omit the Z-axis, or define a constant for it, and it should work.
So is this script attached to the gameObject that you instansiate?
– Filhanteraren