Stop Spawning

I know there’ll be a performance hit with using FindObjectsOfType each frame, but I didn’t like the idea of the LevelController been “controlled”, I feel it should be doing the “controlling”.
So I did this:

public class LevelController : MonoBehaviour
{
    private int numberOfAttackers = 0;
    private GameTimer gameTimer;
    // Start is called before the first frame update
    void Start()
    {
        gameTimer = FindObjectOfType<GameTimer>();
    }

    // Update is called once per frame
    void Update()
    {
        if (gameTimer.HasTimerFinished())
        {
            AttackerSpawner[] attackerSpawners = FindObjectsOfType<AttackerSpawner>();
            foreach(AttackerSpawner spawner in attackerSpawners)
            {
                spawner.StopSpawning();
            }
        }
        int numberOfAttackers = FindObjectsOfType<Attacker>().Length;
        if ((numberOfAttackers == 0) && (gameTimer.HasTimerFinished()))
        {
            Debug.Log("End Level Now");
        }
    }
}

Controlling scripts is like CEO sitting on an armchair, letting others doing harder work and waiting for information for whenever a task is completed. Then comes his work at managing those completed tasks and giving out new orders.

With your script, instead of waiting, that CEO would be, like, nagging others every second which doing nothing but wasting time resource and making works even more stressful for both sides.

Hope this analogy makes things clear for you.

Then maybe there should be a GameController that has handles to all of the Controllers.

Privacy & Terms