Game Development Reference
In-Depth Information
Removing the Debug.Log statements
When your game is ready to build and ship, remember to
remove or comment away any
Debug.Log
statements for
extra cleanliness.
Overriding the ToString method
The following code sample 2-3 demonstrates the convenience of the
ToString
method when used in conjunction with the
Debug.Log
debugging.
ToString
lets
you convert an object to a human-readable string that can be output to
Console
.
In C#, every class inherits the
ToString
method by default. This means that using
inheritance and polymorphism, you can override the
ToString
method of your
class that customizes it as required and produce a more readable and accurate
debug string that represents your class members. Consider the following code
sample 2-4 that overrides
ToString
. If you get into the habit of overriding
ToString
for every class you make, your classes will become easier to debug:
using UnityEngine;
using System.Collections;
//--------------------------------------------
//Sample enemy Ogre class
public class EnemyOgre : MonoBehaviour
{
//--------------------------------------------
//Attack types for OGRE
public enum AttackType {PUNCH, MAGIC, SWORD, SPEAR};
//Current attack type being used
public AttackType CurrentAttack = AttackType.PUNCH;
//Health
public int Health = 100;
//Recovery Delay (after attacking)
public float RecoveryTime = 1.0f;
//Movement speed of Ogre - metres per second