Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing

Print, Debug.Log, object type, Boxing & Unboxing

Notes:

I. Understanding print method : [Start At = 00min:55sec]
II. Understanding object type : [Start At = 04min:00sec]
III. Understanding boxing and unboxing : [Start At = 12min:06sec]
IV. Understanding Debug.Log method : [Start At = 22min:30sec]

I. Uderstanding print method : [Start At = 00min:55sec]

using UnityEngine;
public class FirstScript : MonoBehaviour {
void Start ()
{
print ("Hello Unity");
}
}

print : Logs message to the Unity Console (identical to Debug.Log).

Syntax of print method:
print method definition: In MonoBehaviour class
public static void print(object message) // method header
{
Debug.Log(message); // method body
}
where:
print is a method name
void is a return type
static indicates it is an instance memeber
public indicates it can be accessed globally
message is a parameter of type object

II. Understanding object type : [Start At = 04min:00sec]
object : is a reference type can hold any type of data, by default null,
message: is a memory location, a box to hold a value

using UnityEngine;
public class FirstScript : MonoBehaviour {
void Start ()
{
object message = "Hello Unity";
print (message);
object num = 22;
print (num);
}
}

III. Understanding boxing and unboxing : [Start At = 12min:06sec]
boxing : wrapping primitive data in an object
unboxing : unwrapping primitive data from an object

using UnityEngine;
public class FirstScript : MonoBehaviour {
void Start ()
{
// boxing : wrapping primitive data in an object
object message = 22;

// unboxing : unwrapping primitive data from an object
// int num = message; // error: Cannot implicitly convert type object to int
int num = (int) message;

print (num);
}
}

IV. Understanding Debug.Log method : [Start At = 22min:30sec]
using UnityEngine;
public class FirstScript : MonoBehaviour {
void Start ()
{
Debug.Log ("Hello Unity");
}
}

Debug.Log : Logs message to the Unity Console.
Debug.Log method definition: In Debug class
public static void Log(object message)
{
Debug.Interna_Log(0,(message==null) ? "Null" : message.ToString(),null);
}
where
Debug.Interna_Log is actually responsible for displaying message to the Console panel.
if message is equal to null then displays Null on the Console
else converts the message to string and displays it.

It is recommended to use Debug.Log method.