Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 5.x 2D Game Development Blueprints

You're reading from   Unity 5.x 2D Game Development Blueprints Explore the features of Unity 5 for 2D game development by building three amazing game projects

Arrow left icon
Product type Paperback
Published in Sep 2016
Publisher Packt
ISBN-13 9781784393106
Length 252 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Francesco Sapio Francesco Sapio
Author Profile Icon Francesco Sapio
Francesco Sapio
Abdelrahman Elsayegh Abdelrahman Elsayegh
Author Profile Icon Abdelrahman Elsayegh
Abdelrahman Elsayegh
Arrow right icon
View More author details
Toc

Game handler


Now that we have all the elements in our game, we need a script that makes our character intractable with the other elements in the level, and that takes care of the level logic.

To do this, create a new C# script and name it GameHandler under the folder Scripts. Then, attach the script to our Player object. Double-click on the script in order to open it.

First, we need to define a lot of variables. Let's start with two to keep track of the player's score and health:

    public float health = 2; 
    public float score = 0; 

Now we need a variable to check if the game is over:

    public bool gameover = false; 

Finally, we need the reference to our UI elements:

    public UnityEngine.UI.Text healthUI; 
    public UnityEngine.UI.Text ScoreUI; 
    public GameObject gameOverUI; 
    public GameObject youWinUI; 

The next step is to implement the logic behind the collision of the character with an object. This can be done by using a function called...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime