- Home /
Is there a logging method, similar to C#'s Console.WriteLine? (what I want is the {N})
Hello guys,
I was just testing something when I accidentally wrote:
Debug.Log("There are {0} elements in my list", list.Count);
Then I remembered I can't do that in Debug.Log
- And I can't use Console.WriteLine
to output to Unity's console (can I?)
Is there a way of doing this? Does Unity have a logging method, that supports this? or do I have to try to implement this thing myself? - Not that it's not doable, but won't be too easy.
Thanks.
Answer by phxvyper · Nov 10, 2013 at 05:53 PM
The {N} function used in Console.WriteLine is the same exact thing used in String.Format.
For example:
Debug.Log(String.Format("Hello {0}, you are {1} years old.", name, age));
Answer by Eric5h5 · Nov 10, 2013 at 05:53 PM
Use System.String.Format.
Debug.Log (String.Format("There are {0} elements in my list", list.Count));
Answer by tw1st3d · Nov 10, 2013 at 05:47 PM
Debug.Log("There are " + list.Count + " elements in my list");
Thanks for your reply ^^ - I know about that of course. But in WriteLine
you could do stuff like:
Console.WriteLine("Hello, my name is {0}, I'm {1} years old, and I like {2}", name, age, hobby);
Very neat, very elegant, cleaner and easier to write!
As apposed to:
Debug.Log("Hello, my name is " + name + " I'm " + age + " years old, and I like " + hobby);
I hope you're feeling me now...
I feel like you may be talking about C#'s formatting system, in which case this answer holds no merit. However, there's another solution which may help (that I can't describe perfectly), but it's using % to parse what's being called.
It's something like
Debug.Log('%s%n%s', "There are ", list.Count, " elements in my list");
I may be off on what the letters are, but I was just going with %s = string, %n = number. %n isn't right, but I don't know what's supposed to be there, as I find this method of printing extremely useless.
Don't know about this. I'll check it out. This does seem like C, good old days :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Debug log to website. 2 Answers
How do I change the default application used to open logs from the Unity editor? (on Mac) 1 Answer