Hi guys,
I’m making a game and I need to convert a Float value to String. I tried using Debug.Log("Coins : " + coins.ToString);
but it says I can’t apply a operator like + on string values.
What can I do?
Any help hugely appreciated!
Hi guys,
I’m making a game and I need to convert a Float value to String. I tried using Debug.Log("Coins : " + coins.ToString);
but it says I can’t apply a operator like + on string values.
What can I do?
Any help hugely appreciated!
Try this.
Debug.Log("Coins : " + coins.ToString());
If you want no decimal places, you can use:
Debug.Log("Coins : " + coins.ToString("F0"));
// and if you want some places, change the zero
to the number of decimal places.
The issue here is you’re not calling the ToString function, you’re getting a reference to that function. Instead you want something like this (note the parentheses after ToString).
Debug.Log("Coins: " + coins.ToString());
Or you can use the LogFormat method, which is a bit easier especially when you’re printing multiple variables. It will also use C#'s string formatting to automatically convert things to string for you, so you don’t have to call ToString().
Debug.LogFormat("Coins: {0}", coins);
But as always when printing floats you’ll want to cap the number of digits you get to something sane. You probably don’t need 10 decimal places. So you should be able to do something like this.
Debug.LogFormat("Coins: {0:0.00}", coins);
So… I tried your (of everyone) solution and they work pretty nice, so thank you for the help
You’re welcome, man