O'Reilly logo

Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation by James A. Henley, Matthew Johnson

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

Organizing the Code

While all of our code is functional, there are potentially numerous ways in which we could better organize it. We’ll look at a pair of ways that will help us both reduce the visual clutter of working with a large script and reduce the odds of introducing bugs by centralizing some of our important data.

Using Regions in Code

Use of the #region and #endregion keywords would allow us to break our code up into collapsible sections. Let’s take a look at one of our largest scripts—PlayerController—and see how we could break it down using regions. Open the script and adjust it as shown in Listing 12.1.

Listing 12.1 Regions in the PlayerController Script

public class PlayerController : MonoBehaviour ...

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