Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
2
Question by roamcel · Aug 03, 2011 at 06:21 AM · guidebugfileprint

How do I print messages and debug information to the GUI?

Since I'm trying to debug some obscure and unanswered questions about networking and variables, I'm in need of a client-server debug feature. Being that you obviously can't debug what's running on your server, if you start debugging on the client (unless you have two or more pcs running the project I guess, which I can't accomplish), I would like to know if there's a feature that'd allow you to redirect print output, or debug output.

As of now, you can only see print and debug on the console, and I'd like to know if you can redirect their output/ pipe their output to a different device (as you'd do in perl, or dos, for example).

Of course the ideal would be to print to GUI, but if that's not possible, even printing to a file would help greatly.

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 save · Aug 03, 2011 at 08:10 AM 0
Share

Title is edited to make the question a bit more clear.

4 Replies

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

Answer by save · Aug 03, 2011 at 07:44 AM

There are many solutions to this, one is even brought to you automatically.

The Unity way

The print and debug output is stored in a file called output_log.txt inside the data-folder of your runtime. To work with whats in the log callback in your application use Application.RegisterLogCallback.

Try it out with something like this:

static var myLog : String; private var output : String = ""; private var stack : String = "";

function OnEnable () { Application.RegisterLogCallback(HandleLog); }

function OnDisable () { // Remove callback when object goes out of scope Application.RegisterLogCallback(null); }

function HandleLog (logString : String, stackTrace : String, type : LogType) { output = logString; stack = stackTrace; myLog +="\n"+output; }

function OnGUI () { myLog = GUI.TextArea (Rect (10, 10, Screen.width-10, Screen.height-10), myLog); }

Your way

You could also write your own function to take care of messages:

class p {
    static var pDocument : String;
    static function log (string : String) {
        pDocument+="\n"+string;
    }
}
function OnGUI () {
    myLog = GUI.TextArea (Rect (10, 10, Screen.width-10, Screen.height-10), p.pDocument);
}

Then call it with:

p.log("Hello world");

Then you could use System.IO.File.WriteAllText(filePath, pDocument) to write your own log to disc at any given time if you want to.

Debugging from afar

You also have the possibility to send your data to a server which could take care of sessions - for instance a PHP-server which stores a txt-file with the string sent from a client at any given point. This would be suitable for any of the given solutions above.

Have a look at WWWForm.

Comment
Add comment · Show 5 · 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 save · Aug 03, 2011 at 08:01 AM 0
Share

You could of course also have all clients send the server information with their log callback, which would centralize everything. Then you could even have functions on the server alerting from its clients. It all depends on how important the debug feature is and how big of a project this is.

avatar image save · Aug 03, 2011 at 08:07 AM 0
Share

Just a tip:

Also, when working with text on screen that is updating by itself it is easiest to use a GUI-skin that makes sure the text starts at bottom to update upwards (this way you always see the latest information without the need of a scroll function).

Or just switch the order of

 pDocument+="\n"+string; 

to

 pDocument=string+"\n"+pDocument;
avatar image roamcel · Aug 03, 2011 at 08:18 AM 0
Share

Thanks for the great reply.

avatar image save · Aug 03, 2011 at 08:20 AM 0
Share

Happy debugging! :-)

avatar image roamcel · Aug 03, 2011 at 12:15 PM 0
Share

Very thorough and very useful reply, thanks again.

avatar image
4

Answer by bboysil · Mar 18, 2015 at 03:57 PM

Just attach this to any gameObject: (based on "The Unity Way" section of the answer from save)

 using UnityEngine;
 
 namespace DebugStuff
 {
     public class ConsoleToGUI : MonoBehaviour
     {
 //#if !UNITY_EDITOR
         static string myLog = "";
         private string output;
         private string stack;
 
         void OnEnable()
         {
             Application.logMessageReceived += Log;
         }
 
         void OnDisable()
         {
             Application.logMessageReceived -= Log;
         }
 
         public void Log(string logString, string stackTrace, LogType type)
         {
             output = logString;
             stack = stackTrace;
             myLog = output + "\n" + myLog;
             if (myLog.Length > 5000)
             {
                 myLog = myLog.Substring(0, 4000);
             }
         }
 
         void OnGUI()
         {
             //if (!Application.isEditor) //Do not display in editor ( or you can use the UNITY_EDITOR macro to also disable the rest)
             {
                 myLog = GUI.TextArea(new Rect(10, 10, Screen.width - 10, Screen.height - 10), myLog);
             }
         }
 //#endif
     }
 }
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 fallingmoon · Mar 28, 2016 at 10:37 PM 1
Share

This one is implemented with UGUI and looks better: https://www.assetstore.unity3d.com/#!/content/44935

avatar image buttmatrix · Apr 14, 2016 at 08:48 PM 0
Share

Brilliant, thank you!

avatar image takwin · Feb 21, 2018 at 08:53 PM 0
Share

Hello, I have used your method and everything is displayed the way you have described perfectly. but I'm looking to display the transform and rotation values in real-time, such as a moving car, where its coordinates are changing constantly. how can i see these values changing in the console? if the object (car in my case) is selected in the Hierarchy tab, i can see the values changing inside the inspector, when the project is played. i would like to have the same values but on the console using your GUI translation methode. to debug the position of my object i'm using this script:

========= using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CarPosition : $$anonymous$$onoBehaviour { public GameObject Car; // Use this for initialization void Start () { Debug.Log ("Car Realtime position X " + Car.transform.position.x + " Y " + Car.transform.position.y + " Z " + Car.transform.position.z); Debug.Log ("Car Realtime rotation X " + Car.transform.rotation.x + " Y " + Car.transform.rotation.y + " Z " + Car.transform.rotation.z); print (transform.position); print (transform.localRotation); } // Update is called once per frame void Update () { } } =====

Your help is highly appreciated,

$$anonymous$$any Thanks,

Issam

avatar image
0

Answer by Pressler487 · Jul 26, 2018 at 10:31 AM

Usually by default, a breakpoint is set on program start, and you can then either navigate your code using the buttons at the top of the window, or if you have no code, you can customize your view to let you step through a disassembly of the binary you are looking at.chase bank login

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
avatar image
0

Answer by fathimaneha3108 · Aug 19, 2020 at 11:32 AM

hi, @Pressler487 @bboysil This printed sometext on my screen. I wanted to write this code for this : Say i have to kill 5 villans in my scene and i killed 2, so it must display sometext "2 killed,3 pending", or something and update the progress. Can you please help?,Hi, this wirks and sends default values. I want to use this to print a message on game scene according to happening. Ex : if i have killed 2 villans and 3 are pending, It must display 2/5 killed or something How to modify? Please help, thank you

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

12 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 avatar image avatar image

Related Questions

Debug.log,print not working when run from mobile android 0 Answers

Why is print() only in MonoBehaviour? 1 Answer

Inexplicable variable behaviour - debug help? 1 Answer

How do you print to the console/log from native code on OSX ? 1 Answer

How Do I Display Multiple Strings In Here? 3 Answers


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