Info On Debugging


Is there a way to print a variable to the console as well as showing what has changed the value?


I have a boolean -- var red : boolean = false;

Somewhere in my game, this is changed to true, and for sake of argument, I have over 65 scripts in which I don't want to search through.

How would I print to the console when it changed value, and where it was changed from?

I have a feeling this is a stupid question, and so I apologise in advance.

You can log a message to the console with the following command:

Debug.Log("log this message");

You can add anything you want to the message strings:

Debug.Log("Script/component name is: " +;
Debug.Log("Game object's name is: " +;

Note that if you click on the message in the Unity console window, in the bottom pane it shows you the stack trace, with line numbers. So by clicking you can see which functions called the function that actually generated the message.

Well, finally another argument to use C# :D

In C# you can change a variable into a property and do a Debug.Log in the set-method.


public bool red = false;


private bool m_Red = false;
public bool red
   get { return m_Red; }
      m_Red = value;
      Debug.Log("red has been set to " + m_Red);

But unfortunately there are no properties in JS. You have to use a proper editor (like visual studio) in which you can perform a project wide search to find all occurrences of your variable. Since you called it just "red", that can be frustrating. That's why you should name your variables with meaningful distinctive identifier.