- Home /
Cancel Spawn without Ending Round/Wave
So I've been working with one of the Unity tutorials for a while and have moved on to making additional modifications/core game modules.
I have a wave spawn timer that (for wave 1) lasts/spawns enemies for 30 seconds. Currently my logic is as follows:
Has it been longer than 30 seconds? If so, stop spawning enemies and load the next scene (Wave 2)
At first I thought I could just use invoke to delay the time between stopping the spawns and loading the next level but I encountered a problem. The next round will load even if there are enemies still in the current scene (which isn't a major problem but I would like to force the player to kill all enemies before being able to move on)
using UnityEngine;
public class EnemyManager : MonoBehaviour
{
public PlayerHealth playerHealth;
public GameObject enemy;
public float spawnTime = 3f;
public Transform[] spawnPoints;
private float timer;
void Start ()
{
InvokeRepeating ("Spawn", spawnTime, spawnTime);
timer = Time.time + 30;
}
void Spawn ()
{
if(playerHealth.currentHealth <= 0f)
{
return;
}
if (timer < Time.time) {
CancelInvoke();
Application.LoadLevel(1);
}
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
}
What I would like to do logically is the following:
Has it been longer than 30 seconds? If so, stop spawning enemies. Are there any active enemy gameobjects in the scene? if so do nothing. If there are not then load the next wave.
How do I check to see if there are any active enemy gameobjects in the current scene?
Any help is appreciated as it doesn't seem to be the easiest thing to solve.
I'm aware I might be able to use GameObject.SetActive to set each enemy gameobject as active then use GameObject.activeInHierarchy to somehow check if any of the gameobjects are still active before loading the next wave but I'm struggling with how to put it together.
Answer by AlwaysSunny · Apr 11, 2015 at 03:59 AM
The root of this question is about scene and reference management. It's a question we get half a dozen times a day it seems. I wonder if there's not enough emphasis on this topic in Unity lessons here.
Reference and scene management are important game design fundamentals, but the topic isn't exactly simple. There are alternate styles and preferences, but generally the most convenient way of tracking references wins.
Sometimes the script responsible for spawning an object can keep a collection of those spawned instances. (e.g. the level is cleared if the time is up and the list is empty).
Eventually in a more complicated project, you'll want to explore other more sophisticated ways of keeping track of what's going on in your game.
The lessons provided by Unity (and external sources) seem to gloss over management of scene objects and I have not found a decent reference on how to manage multiple objects (especially multiple enemy "types")
I ended up using the score counter to do it. if wave 1 contains 10 enemies, and killing ten enemies gives you a score of 1000, then I just check the players score, if not 1000 then do nothing, if 1000 then load wave 2. This seems like a very "hackish" way to do it but until I can find a tutorial or video that shows me otherwise I can't do much more.
Your answer
Follow this Question
Related Questions
Want to spwan obstacle from top to down in a 2D top to down game. 0 Answers
Spawn multiple enemies in relation to gameobject [SOLVED] 1 Answer
(2D) Spawn enemies off-screen and have them to follow the Player as a target 0 Answers
SpawnEnemies script slows the game significantly after just 40 or so? 1 Answer
Spawn Point 2 Answers