O'Reilly logo

Unity Game Development Scripting by Kyle D'Aoust

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Final preparations

Now, we will add our final features to make this game complete.

Adding win conditions

Right now, the game is playable, but there is no way to win! Let's change that by creating a new empty GameObject and naming it RoundManager. Next, create a new C# script, name it WinConditions, and add this code to the script:

public int Enemies;

void Start ()
{
  GameObject[] e = GameObject.FindGameObjectsWithTag("Enemy"); 
  Enemies = e.Length;
}

void Update ()
{
  if(Enemies <= 0)
  {
    if(Application.loadedLevel != 3)
      Application.LoadLevel(Application.loadedLevel + 1);
    else
      Application.LoadLevel(0);
  }
}

What this script will do is get a count of how many enemies there are in the scene and use that information to decide whether the player wins. In the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required