This article is Part 12 in a 25-Part Series.

I intend to cover the part of the Game Manager handling the spawning of player and enemies. Then I realized it would be tough to explain without going through the Level Manager first. The Level Manager is like a level editor of sorts where we use to specify the differences between each level. The differences are the number of each type of enemy tanks in the level and also the Stage Number.

So whenever we create a new Stage, other than painting the tilemaps, all we need to do is edit the number of tanks and the Stage Number and the scene will be done. The GamePlayManager can then read from the Level Manager what is the Stage Number and display it accordingly at the Start of Stage.

Just code and nothing else

Level Manager is only about code as it is used to store information (albeit within the Level unlike MasterTracker which is throughout the Game). Start by creating a C# script under the Canvas Game Object called LevelManager.

  • Create 4 Public Static Int Variable (smallTanks, fastTanks, bigTanks, armoredTanks); this is used to keep track the number of each type of tanks that can be spawned on the Stage.
  • Create 5 SerializedField Int Variable (smallTanksInThisLevel, fastTanksInThisLevel, bigTanksInThisLevel and armoredTanksInThisLevel and stageNumberInThisLevel). This is for us to set the number of each type of tanks in each stage and stage number.

The only code execution here is

  1. The initial number of tanks passed from the SerializedField Ints to the Public Static Ints.
  2. Pass the value of stageNumber to the static int stageNumber of MasterTracker
  3. This will be put in the Awake Monobehaviour to ensure the values are passed in as soon as possible.

Below is all the code that is mentioned above.

using UnityEngine;

public class LevelManager : MonoBehaviour {
    [SerializeField]
    int smallTanksInThisLevel, fastTanksInThisLevel, bigTanksInThisLevel, armoredTanksInThisLevel, stageNumber;
    public static int smallTanks, fastTanks, bigTanks, armoredTanks;
    private void Awake()
    {
        MasterTracker.stageNumber = stageNumber;
        smallTanks = smallTanksInThisLevel;
        fastTanks = fastTanksInThisLevel;
        bigTanks = bigTanksInThisLevel;
        armoredTanks = armoredTanksInThisLevel;
    }
}

With the LevelManager ready, we can move on to the GameManager again to discuss how we can trigger the spawning of enemies and player.

This article is Part 12 in a 25-Part Series.