Book Image

Unity 2D Game Development Cookbook

By : Claudio Scolastici
Book Image

Unity 2D Game Development Cookbook

By: Claudio Scolastici

Overview of this book

<p>Unity is a powerful game development engine that provides rich functionalities to create 2D and 3D games.</p> <p>Unity 2D Game Development Cookbook is a practical guide to creating games with Unity. The book aims to serve the purpose of exploring problematic concepts in Unity for 2D game development, offering over 50 recipes that are easy to understand and to implement, thanks to the step-by-step explanations and the custom assets provided. The practical recipes provided in the book show clearly and concisely how to do things right in Unity. By the end of this book, you'll be near "experts" when dealing with Unity. You will also understand how to resolve issues and be able to comfortably offer solutions for 2D game development.</p>
Table of Contents (15 chapters)
Unity 2D Game Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Setting game exit conditions – goals met


To set the winning conditions for the level, we need to count the items collected and check that value against a default number of items we want the level to end with. Once the number of items collected equals the goal, the player receives a message.

This is the topic of our next recipe.

Getting ready

We have already counted the number of items collected in the runner script, through the collected int variable. Thus we can take advantage of the code we already implemented and improve it to manage the additional functionality.

How to do it…

  1. Open the runner script in Monodevelop. In the upper section with the variable declaration, modify the declaration for collected, as shown here:

      public int collected, levelGoal;
  2. In the Start() function, add the initialization for levelGoal with the value of 5:

      collected = 0;
      levelGoal= 5;
  3. In the Update() function, add the following lines to check whether the winning conditions are met, and send a message to the player...