Tracing or Debugging
Notes:
Debugging or Tracing C# scripts in Unity (Game Engine):
I. Tracing using Debug.Log method : [Starts at:00min:00sec]
II. Tracing using MonoDevelop's Debugger tool : [Starts at:18min:15sec]
III. Tracing using Paper, Pencil and Eraser : [Starts at:26min:50sec]
Debugging or Tracing:
refers to executing the code or the part of a code line by line
performed for locating bugs (logical errors) and removing them
3 ways of tracing a program:
Using Debug.Log method
Using MonoDevelop's Debugger tool
Using Paper, Pencil and Eraser
I. Tracing using Debug.Log method : [Starts at:00min:00sec]
We can log messages after every step to examine the correct behavior (logic) of the code
Note:
We will remove Log messages from the final product
Debug.Log is used to examine what exactly happening to variables behind the scene.
Example Code:
using UnityEngine;
public class FirstScript : MonoBehaviour {
// Use this for initialization
void Start ()
{
// datatype variableName
int numOfBullets=100; // declaration and initialization
Debug.Log ("Num of Bullets= " + numOfBullets); // 100
int numOfGraneds = 300; // declration initialization
Debug.Log ("Num of Graneds= " + numOfGraneds); // 300
int totalWeapons = numOfBullets + numOfGraneds;
Debug.Log ("Total Weapons= " + totalWeapons); // 400
// player has thrown all graneds
numOfGraneds = 0;
Debug.Log ("Num of Bullets= " + numOfBullets); // 100
Debug.Log ("Num of Graneds= " + numOfGraneds); // 0
//Debug.Log ("Total Weapons= " + totalWeapons); // 400
/*
* we must get 100 ask why we are getting 400
* */
// Answer is: we must recalculate total weapons to get correct result
totalWeapons = numOfBullets + numOfGraneds;
Debug.Log ("Total Weapons= " + totalWeapons); // 100
// player has collected 100 more bullets
numOfBullets = numOfBullets + 100;
Debug.Log ("Num of Bullets= " + numOfBullets); // 200
Debug.Log ("Num of Graneds= " + numOfGraneds); // 0
//Debug.Log ("Total Weapons= " + totalWeapons); // 100
/*
* we must get 200 ask why we are getting 100
* */
// Answer is: we must recalculate total weapons to get correct result
totalWeapons = numOfBullets + numOfGraneds;
Debug.Log ("Total Weapons= " + totalWeapons); // 200
}
}
II. Tracing using MonoDevelop's Debugger tool : [Starts at:18min:15sec]
MonoDevelop's Debugger:
help us to debug logical errors present in the code.
Steps:
1. Hit the play button in MonoDevelop to attach (or Detach) the debugger
2. Place (Remove) the breakpoint just by clicking in the gutter area present beside the line number
3. Hit the play button in the Unity IDE to start (Stop) the execution
Execution pauses at the breakpoint
4. Hit the step over button for line by line execution
Note:
In the locals window you can examine local variables
In the call stack you can examine function calls
In the MonoDevelop's View menu you find Debugging windows
Example Code:
using UnityEngine;
public class FirstScript : MonoBehaviour {
// Use this for initialization
void Start ()
{
// datatype variableName
int numOfBullets; // declaration
numOfBullets = 100; // initialization
int numOfGraneds = 300, totalWeapons; // declration initialization
totalWeapons = numOfBullets + numOfGraneds;
Debug.Log ("Num of Bullets= " + numOfBullets); // 100
Debug.Log ("Num of Graneds= " + numOfGraneds); // 300
Debug.Log ("Total Weapons= " + totalWeapons); // 400
// player has thrown all graneds
numOfGraneds = 0;
totalWeapons = numOfBullets + numOfGraneds;
Debug.Log ("Num of Bullets= " + numOfBullets); // 100
Debug.Log ("Num of Graneds= " + numOfGraneds); // 0
Debug.Log ("Total Weapons= " + totalWeapons); // 100
// player has collected 100 more bullets
numOfBullets = numOfBullets + 100;
totalWeapons = numOfBullets + numOfGraneds;
Debug.Log ("Num of Bullets= " + numOfBullets); // 200
Debug.Log ("Num of Graneds= " + numOfGraneds); // 0
Debug.Log ("Total Weapons= " + totalWeapons); // 200
}
}
III. Tracing using Paper, Pencil and Eraser : [Starts at:26min:50sec]
In the memory there is stack area (Call Stack).
Stack area is used to maintain function calls.
Whenever the control enters the function body,
that function is pushed on top of the stack and
its local variables are created with default values.
When the control leaves the function body,
that function is poped out of the stack and
its allocated variables are deleted from the stack.