Handling Exceptions

Exceptions are the glaring errors thrown by problematic code. They show themselves in the Unity Console, which provides us with a snippet of information about the error and a link to the script line where it encountered the error.

Open the PlayerController script and scroll down to the following line:

float move = Input.GetAxis("Horizontal");

Remove a letter from the word "Horizontal". Any letter. Take your pick. We know that the line is no longer correct, but the script is still going to build because there are no syntax errors present. Save the script, return to Unity, and run the game. You’ll find that the Console is immediately plagued with error messages like those seen in Figure 5.5.

Get Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.