Reader small image

You're reading from  Hands-On Unity 2021 Game Development - Second Edition

Product typeBook
Published inAug 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781801071482
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Nicolas Alejandro Borromeo
Nicolas Alejandro Borromeo
author image
Nicolas Alejandro Borromeo

Nicolas is a Game Developer currently working as a Senior Software Development Consultant for Unity in London. He is a Unity Certified Instructor teaching Unity clients all around the globe. He started using Unity in 2008 and teaching it in 2012 in several Universities and Education Institutes.
Read more about Nicolas Alejandro Borromeo

Right arrow

Improving our code with events

So far, we have used Unity event functions to detect situations that can happen in the game, such as Awake and Update. These functions are ways for Unity to communicate two components, as in the case of OnTriggerEnter, which is a way for the Rigidbody to inform other components in the GameObject that a collision has happened. In our case, we are using if statements inside Updates to detect changes on other components, such as GameMode checking whether the number of enemies reached 0. But we can improve this if we are informed by the Enemy Manager when something has changed, and just do the check in that moment, such as with the Rigidbody telling us the collisions instead of checking collisions every frame.

Also, sometimes, we rely on Unity events to execute logic, such as the score being given in the OnDestroy event, which informs us when the Object is destroyed, but due to the nature of the event, it can be called in situations where we don&apos...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Hands-On Unity 2021 Game Development - Second Edition
Published in: Aug 2021Publisher: PacktISBN-13: 9781801071482

Author (1)

author image
Nicolas Alejandro Borromeo

Nicolas is a Game Developer currently working as a Senior Software Development Consultant for Unity in London. He is a Unity Certified Instructor teaching Unity clients all around the globe. He started using Unity in 2008 and teaching it in 2012 in several Universities and Education Institutes.
Read more about Nicolas Alejandro Borromeo