Formatted output with Debug.Log()

I am not getting any straight answers from google that work.

How do you do the equivalent of C++ printf(“%d”, nNum);

1 Like

For individual values (each type takes its own format possibilities):
Debug.Log(nNum.ToString(…));

For multi-parameter strings:
Debug.Log(string.Format(…));

Bookmark the MSDN webpage, refer to it whenever you want, as it contains the documentation of the entire .Net framework, as well as example uses.

It’s a start.

Any idea where in all that it explains the syntax of the formating…along the lines of what %d means with printf.

Debug.LogFormat("Number: {0}, string {1}, number again: {0}, character: {2}", num, str, chr);
8 Likes

The format strings depend on the datatype being formatted.

google: “.net numeric format strings”

google: “.net datetime format strings”

You may want to look into practicing your google-fu.

Nevermind that the article I linked originally referenced both of these links here.

Just to update this to be more modern:

int count = 5;
Debug.Log($“I have {count} cakes”);

this will output “I have 5 cakes”

5 Likes

int myInt = 10;;
float myFloat = 2.0f;;
object[ ] args = {myFloat, myInt};
Debug.LogFormat(“My float is {0} and my int is {1}”, args);

Although this works fine, this is not very readable in my opinion. Why might this better than string interpolation?

Just personal preference. I typically do

Debug.Log("Count = " + count.ToString());