Debug.Log(); or print();? What's the difference and when to use what?


print(); or even

System.Console.WriteLine(); (which only writes to the Editor-Logfile, not to the console),

or maybe others I may not be aware of…

Are there any rules as to when to use what?

Any reason for the (seeming) redundancy?

I found this related question with some very good answers:

To quote Jashans answer:

[...] the reason is very likely that log statements are somehow batched for performance reasons (e.g. only putting out logging "once per frame" which would mean all log-statements from one frame are sent to the console in a single step [...]

Can anyone comment on that?

Are there any other pitfalls to be aware of?

Like one of these not producing any output under particular circumstances or something?

Thanks & Greetz, Ky.

I’m surprised no one mentioned this.

print() only works if your class inherits from MonoBehavior.

If it doesn’t your only choice is to use Debug.Log().

There is no functional difference between Debug.Log() and print(). print() simply wraps the Debug.Log command, and is in effect just an easier-to-remember alias to the same command. Because it's a function on the MonoBehaviour class, it means that it does not need to be preceded by an object reference when writing monobehaviours.

Whether or not Debug.Log messages are batched in builds to improve performance (I don't know about that), it's best to completely remove them once you're happy with your final build - particularly any which are printed during you game update functions!

On the reference page of the class Monobehaviour, is write that the print function is identical do Debug.Log:

                   BlockquoteLogs message to the Unity Console (identical to Debug.Log).

If you examine UnityEngine.CoreModule.dll with ILSpy, you’ll notice that MonoBehaviour.print(object message) calls Debug.Log(object message).