- Home /
spawn timer problem
Somehow i can`t manage to make this enemys spawn with timer. THey do spawn as soon as i kill one, but it happens INSTANTLY, despire WaitForSeconds being in script. Need an idea how to make new guy appear like 2 seconds after you kill the one before him, not instantly. Any help would be appritiated.
using UnityEngine;
using System.Collections;
public class SpawnEnemy : MonoBehaviour
{
public GameObject[] EnemyTypes;
ScoreManager scoreManager;
void Awake()
{
scoreManager = GameObject.Find("GameManager").GetComponent<ScoreManager>();
}
void Update()
{
if (scoreManager.inPlay)
{
if (scoreManager.EnemyCount <= 3)
Spawn();
}
}
void Spawn()
{
Debug.Log("SPAWN!");
Vector3 spawnV = new Vector3(Random.Range(-6, 6), Random.Range(-3, 3), 0);
int s = Random.Range(0, EnemyTypes.Length);
Instantiate(EnemyTypes[s], spawnV, Quaternion.identity);
//Instantiate(EnemyTypes[0], Vector3.zero, Quaternion.identity);
scoreManager.EnemyCount++;
StartCoroutine("Wait");
}
IEnumerator Wait()
{
yield return new WaitForSeconds(2);
}
}
Answer by JoshDangIt · May 16, 2016 at 01:50 AM
You aren't using the co-routine properly. Try this instead (Untested):
using UnityEngine;
using System.Collections;
public class SpawnEnemy : MonoBehaviour {
public GameObject[] EnemyTypes;
public float spawnRate = 2f; //probably don't want to hardcode that
ScoreManager scoreManager;
float timeSinceLastSpawn = 0f;
void Awake()
{
scoreManager = GameObject.Find("GameManager").GetComponent<ScoreManager>();
}
void Update()
{
timeSinceLastSpawn += Time.deltaTime;
if (scoreManager.inPlay)
{
if (scoreManager.EnemyCount <= 3 && timeSinceLastSpawn >= spawnRate)
Spawn();
}
}
void Spawn()
{
Debug.Log("SPAWN!");
Vector3 spawnV = new Vector3(Random.Range(-6, 6), Random.Range(-3, 3), 0);
int s = Random.Range(0, EnemyTypes.Length);
Instantiate(EnemyTypes[s], spawnV, Quaternion.identity);
//Instantiate(EnemyTypes[0], Vector3.zero, Quaternion.identity);
scoreManager.EnemyCount++;
timeSinceLastSpawn = 0f;
}
}
Works like a charm, thanks. $$anonymous$$ind ic i`ll ask for your assistance again? Can you explain, why my code(before editing) used to spawn max amount of enemys per spawn cycle? $$anonymous$$ean if I set it to <=3 I produced 3 new ones even if only 1 was killed)
I'll try but it's kind of hard to explain.
When you used StartCoroutine(), nothing was stopping the Update function from continuing to be called. Coroutines run along side all the other functions, so your wait function didn't actually wait for anything.
Will need to get along with corutines. $$anonymous$$ind of strange how they work sometimes) : Thanks again.
Answer by 5c4r3cr0w · May 16, 2016 at 06:52 AM
Replace Spawn() in Update() to your wait coroutine like :
void Update()
{
timeSinceLastSpawn += Time.deltaTime;
if (scoreManager.inPlay)
{
if (scoreManager.EnemyCount <= 3 && timeSinceLastSpawn >= spawnRate)
StartCoroutine(Wait());
}
And then call Spawn function in that coroutine like :
IEnumerator Wait()
{
yield return new WaitForSeconds(2);
Spawn();
}
This will pretty much do your job. Good luck.