I want my prefab “enemy” to spawn every 3 seconds after the previous enemy has been destroyed. Currently my code doesn’t work because of two things. Firstly, I can’t destroy the enemy upon death because that would disable the script. Secondly, I can’t use WaitForSeconds(3) between the death and spawn because that can’t be called in an update function. I also can’t use InvokeRepeating because I want it to spawn my variable enemy prefab and when I use InvokeRepeating it spawns saying that the variable enemy is missing the prefab i dragged in. Here is my code:
#pragma strict
var enemyHealth = 10;
var clickDamage = 5;
var enemy : GameObject;
var spawnWaitTime = 2;
function Start() {
//InvokeRepeating ("Spawn", 1, spawnWaitTime);
}
function Update() {
if(Input.GetMouseButtonDown(0)) {
enemyHealth -= clickDamage;
if (enemyHealth <= 0) {
Death();
}
}
}
function Death() {
//gameObject.renderer.enabled = false;
Destroy(gameObject);
WaitForSeconds(3);
Spawn();
}
function Spawn() {
var newEnemy = Instantiate(enemy, transform.position, transform.rotation);
}