• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
46
Question by SisterKy · Jan 14, 2010 at 12:05 AM · debug.logprint

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

Debug.Log();
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:
http://answers.unity3d.com/questions/650/debug-log-doesnt-work-properly-in-a-contextmenu-function
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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SisterKy · Aug 03, 2011 at 09:11 PM 0
Share

cross-reference http://answers.unity3d.com/questions/22196/where-is-print.html#

4 Replies

· Add your reply
  • Sort: 
avatar image
35
Best Answer

Answer by duck · Jan 14, 2010 at 12:42 PM

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!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Max Kaufmann · Jan 14, 2010 at 05:38 PM 0
Share

In flash there's a publish option to NOOP all trace() calls, rather than manually commenting them out. Is there something like that in Unity?

avatar image duck ♦♦ · Jan 15, 2010 at 10:09 AM 0
Share

I don't think so. However to implement this you could write your own function which wraps the Debug.Log function, with an addition 'enabled' boolean which you could switch on/off.

avatar image liortal · Jun 17, 2014 at 05:21 PM 2
Share

You could also create a new Log() method that is marked with the ConditionalAttribute("DEBUG"). The compiler will remove all calls to this method in case DEBUG symbol is not defined.

avatar image Eric5h5 · Jun 17, 2014 at 05:38 PM 1
Share

Unity since has had a "Use player log" option, so you can simply turn that off.

avatar image
80

Answer by Santokes · Dec 21, 2011 at 06:20 AM

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().

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mehow · Mar 25, 2014 at 03:25 PM 5
Share

I feel this should be the accepted answer actually. It is also worth mentioning that Debug.Log() is capable of taking 2 parameters while print() only takes one. The second parameter in the Debug.Log works kind of like .Assert as it acts like a condition is null. Well, also Debug is a class itself that was specifically designed and implemented to help developers with debugging code. It provides more static functions that help catching bugs. The poor print seems to be only working with classes derived from $$anonymous$$onoBehaviour. It seems it has been implemented long time ago and Debug class has replaced it extending the functionality and possibilities.

avatar image _watcher_ · Nov 10, 2014 at 03:47 PM 0
Share

It was mentioned here: "Because it's a function on the $$anonymous$$onoBehaviour class, ..."

avatar image toddlegare · Jul 13, 2016 at 07:58 PM 1
Share

If your class does not inherit from $$anonymous$$onoBehavior, you can also use:

$$anonymous$$onoBehaviour.print()

It will behave like Debug.Log().

avatar image
4

Answer by jpsmarinho812 · Jul 31, 2014 at 08:47 PM

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).
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Jul 31, 2014 at 11:53 PM 0
Share

Except it's not, as Santakes' answer indicates, and mehow's comment below that elaborates on.

avatar image
2

Answer by dabalciunas · Jun 23, 2019 at 07:52 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity's Equivalent of Console.Write(); 1 Answer

Unity Console not showing result 1 Answer

Render Debug.Log as UI Text 2 Answers

[Resolved] Why unity print 0.08f + 0.02f as 0.0999999 instead of 0.1? 1 Answer

Is it possible to make a console entry (debug.log/print statement) with more than 2 lines of consecutive content? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges