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.

3 Answers

3

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));

Use System.String.Format.

Debug.Log (String.Format("There are {0} elements in my list", list.Count));

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 :)