Intro to Script Communication — Stop Spawning when dead

We’ve got our Enemies spawning all neat and tidy, but we have a problem — they keep spawning when our Player Cube is destroyed!

To solve this, we’ll have to tell our Spawn Manager to stop spawning when our Player isn’t alive — when the game is over. We’ll create a bool, and communicate with our Spawn Manager from inside our Player script. This will lightly touch Script Communication, which will be discussed quite often as it’s a paramount concept to understand.

The Problem

Enemies keep spawning when our Player is destroyed.

The Solution

The best way to know if the Game is over is to simply create a bool. In our case, we’ve already created this bool!

In our SpawnManager script, we’ve created the _isGameActive bool and we initialized it to be true. This is being used in our SpawnRoutine() — while _isGameActive is true, it will perform the coroutine and Spawn Enemies.

Now we need a way to turn if off, thus stopping the enemies from being spawned.

In our SpawnManager script, we’ll create a public method, OnPlayerDeath(), and in that we’ll set our _isGameActive to false.

Now, inside out Player script, we’ll have to call the public method OnPlayerDeath() when our lives = 0.

The green text is our pseudo-code. So we know where and why to call our new method.

To do that, we’ll need to create a handle to reference our Spawn Manager. So we’ll create a Global variable of type SpawnManager called _spawnManager.

But instead of linking that reference in the Inspector like we have previously done, we’ll now explicitly tell our code where to look!

In Start(), we use _spawnManager = GameObject.Find(“Spawn Manager”).GetComponent<SpawnManager>().

Then check if our connection was properly made by null-checking _spawnManager.

  • GameObject.Find(“<string reference>”) — is looking for a Game Object called “Spawn Manager”. This is a string reference, so it needs to be typed EXACTLY how the Game Object is named in the Inspector.
  • .GetComponent<SpawnManager>() — is getting the component of the SpawnManager script. Our script attached to the game object is component.

Now with the proper script reference, we can communicate with any public variable or method inside of the SpawnManager script!

This is why it’s a good idea to keep things private, unless you need them public for a reason!

So now when our Player’s _lives = 0, we call the OnPlayerDeath() method from the SpawnManager.

This turns _isGameActive to false, effectively stopping our SpawnRoutine() from spawning more enemies!

No more infinite spawning!

There you have it — an introduction to Script communication! It can get a lot more complicated, and it will, but to just grasp the essence of creating a reference and getting a component for that reference is a great stepping stone for C# Programming inside of Unity!

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles