Debug.Log() Is Your Friend

The Debug() function we used in our Try-Catch-Finally block has many uses for debugging. You can slip it into your code to test and examine values at runtime. For instance, let’s say that we want to know whenever the player has pushed the Jump button but the character is already in the air. Debug.Log() gives us a quick and dirty way to get that information. Try adjusting the code in our PlayerController script as shown in Listing 5.7.

Listing 5.7 Adding Debug.Log() to the Update() Method

void Update(){  if(Input.GetButtonDown("Jump"))  {    if(isGrounded == true)    {      this.rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x,0);      this.rigidbody2D.AddForce(new Vector2(0, jumpForce)); ...

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.