- Home /
How do I write to the console?
I'm writing some C# scripts for a Windows Standalone application in Unity. I'd like to print some debug messages to the Unity console, but I can't figure out how. I've read that I should be able to do this using print()
, Debug.Log
, or System.Console.WriteLine()
, but the first two just output to the log file, not the console, and the last doesn't appear to do anything at all.
I'm new to both Unity and C#, so don't hesitate to mention potential solutions that should be obvious enough for me to already have tried them :)
For the consolation prize...
As a workaround, I've been using BareTail to watch the log file, but it kinda sucks because Unity outputs the whole stack trace every time I print()
. Any way for me to suppress that?
Answer by duggulous · Aug 26, 2010 at 09:18 PM
It seems that the key piece of information I was missing was that Debug.Log writes to the console only when you run the game from within unity by pressing the "play" button above the game view.
I was running my game by pressing Ctrl-B, or pressing "Build & Run" in the Build Settings window. In that case, it will only output to the log file.
They are here
or here
Is there really a need to be a jerk? Your suggestions are how I ended up here. Please consider consolidating useful information to a single location in the future ins$$anonymous$$d of endorsing a distributed mess of incomplete steps.
For future searchers: the following link describes the location of the log file on multiple platforms. Worked for me on a mac.
http://answers.unity3d.com/questions/9739/how-can-i-find-editor-log-file.html
Unity has created a helpful page for where to find the log files on all the different platforms. You can find that page here:
http://docs.unity3d.com/Documentation/$$anonymous$$anual/LogFiles.html
8 years later, when looking (and Googling') for a simple answer this is what comes up, and yet still you get *'s giving crap answers, a simple 'I don't know' or a pointer in the right direction is the decent response, not sarcastic links. I really hope that that person left the Unity forums and went back to Stackexchange where comments like that are the norm
Answer by Eric5h5 · Aug 17, 2010 at 10:28 PM
print is an alias for Debug.Log, which does appear in the Unity console. There's also Debug.LogWarning and Debug.LogError, which (strangely enough) print lines labeled as warning and error messages. The latter is particularly useful if you have Error Pause selected in the console.
I'm not getting any output in the Unity console when I call Debug.Log
The output IS showing up in the log file, so I know the code is being called.
If is showing in log file and not showing in console you might have filters turned on (top right of console)
I'm having the same problem. Neither debug.log nor console.writeline have ever worked for me. I use print, cause it works every time. I don't know why.
Answer by rainbow_design · Sep 12, 2015 at 07:12 PM
It seems that the debug.log and console.writeline is routed who knows where.
There is a little snippet to reroute console.writeline it to the real Unity console. I tried it it does work:
http://jacksondunstan.com/articles/2986
using System;
using System.IO;
using System.Text;
using UnityEngine;
/// <summary>
/// Redirects writes to System.Console to Unity3D's Debug.Log.
/// </summary>
/// <author>
/// Jackson Dunstan, http://jacksondunstan.com/articles/2986
/// </author>
public static class UnitySystemConsoleRedirector
{
private class UnityTextWriter : TextWriter
{
private StringBuilder buffer = new StringBuilder();
public override void Flush()
{
Debug.Log(buffer.ToString());
buffer.Length = 0;
}
public override void Write(string value)
{
buffer.Append(value);
if (value != null)
{
var len = value.Length;
if (len > 0)
{
var lastChar = value [len - 1];
if (lastChar == '\n')
{
Flush();
}
}
}
}
public override void Write(char value)
{
buffer.Append(value);
if (value == '\n')
{
Flush();
}
}
public override void Write(char[] value, int index, int count)
{
Write(new string (value, index, count));
}
public override Encoding Encoding
{
get { return Encoding.Default; }
}
}
public static void Redirect()
{
Console.SetOut(new UnityTextWriter());
}
}
To use it put the sample in assets and write UnitySystemConsoleRedirector.Redirect(); in your c# file
Answer by wesfrank_ · Oct 25, 2020 at 04:36 PM
This video explains it well: How To Print To The Unity Console
Answer by DSivtsov · Dec 16, 2021 at 07:25 PM
You can use Workaround when you use the System.Console.WriteLine()
In reality the Unity editor save it to it log file. but doesn't show it in the standard Unity Debug.Console Common place of this log is C:\Users\\AppData\Local\Unity\Editor (Editor.log). You can watch it by every text Editor (more helpful Notepad++, because it can automatically update the showing content). Unity Editor also send to this file the all output of its Debug.Console (in summary its look little weird but for short checking is normal)