Skip to content

Instantly share code, notes, and snippets.

@niorad
Last active August 29, 2015 14:16
Show Gist options
  • Save niorad/61b66f9c5ba09b41db13 to your computer and use it in GitHub Desktop.
Save niorad/61b66f9c5ba09b41db13 to your computer and use it in GitHub Desktop.
Prefab Instantiation Script for Unity3D. Has rounds and pauses between rounds.
using UnityEngine;
using System.Collections;
public class SpawnPrefab : MonoBehaviour {
public Transform prefabSpawnee;
public float interval = 1;
public int instancesPerRound = 5;
public float pauseBetweenRounds = 3f;
private int shotInRound = 0;
private bool isActive = true;
void Start () {
ActivateSpawner();
}
void Spawn () {
if(isActive) {
Transform prefabInstance;
prefabInstance = Instantiate(prefabSpawnee, transform.position, transform.rotation) as Transform;
shotInRound++;
}
}
void ActivateSpawner() {
InvokeRepeating("Spawn", 0, interval);
}
void Update() {
if(shotInRound == instancesPerRound) {
CancelInvoke("Spawn");
shotInRound = 0;
Invoke ("ActivateSpawner", pauseBetweenRounds);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment