Book Image

Unity Game Development Scripting

By : Kyle D'Aoust
Book Image

Unity Game Development Scripting

By: Kyle D'Aoust

Overview of this book

Table of Contents (17 chapters)
Unity Game Development Scripting
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 Start function, we grab the amount of enemies and assign it to the int variable we created. In the Update function, we check that there are no more enemies left. In our game, when you kill all the enemies you move...