Errors when adding a GUI.Label C#

I’m not sure what I’m doing wrong here. For the String parameter I have also tried with a .ToString on both calls.

GUI.Label( new Rect(Screen.width/2, Screen.height*0.2, 150, 50), "$" + Money);
GUI.Label( new Rect(Screen.width*0.1, Screen.height*0.2, 150, 50), Depth + " ft.");

Any help on solving what I’m doing wrong would be greatly appreciated! Here are my variables used.

int Money = 50;
int Depth = 0;

I would really like to keep the variables int’s as well.
Here are the error’s.

error CS1502: The best overloaded method match for `UnityEngine.Rect.Rect(float, float, float, float)’ has some invalid arguments

error CS1503: Argument #2' cannot convert double’ expression to type `float’

error CS1502: The best overloaded method match for `UnityEngine.GUI.Label(UnityEngine.Rect, string)’ has some invalid arguments

error CS1503: Argument #1' cannot convert object’ expression to type `UnityEngine.Rect’

error CS1502: The best overloaded method match for `UnityEngine.Rect.Rect(float, float, float, float)’ has some invalid arguments

error CS1503: Argument #1' cannot convert double’ expression to type `float’

error CS1502: The best overloaded method match for `UnityEngine.GUI.Label(UnityEngine.Rect, string)’ has some invalid arguments

error CS1503: Argument #1' cannot convert object’ expression to type `UnityEngine.Rect’

As the error says, you need float as parameters, not double.
To put a float value, you need to add ‘f’ at the end of the number.

  • 1.0 double
  • 1.0f float

Try this:


GUI.Label( new Rect(Screen.width/2f, Screen.height*0.2f, 150f, 50f), "$" + Money);
GUI.Label( new Rect(Screen.width*0.1f, Screen.height*0.2f, 150f, 50f), Depth + " ft.");

Your error says that Rect is expecting a float but it got something different. The compiler think you’re passing in double numbers instead of float. Basically there are different size decimal numbers in the computer…

For all of your decimal numbers, you need to use an f on the end like 0.2f . When you type 0.2 the compiler thinks its a double, but rect takes in floats.

GUI.Label( new Rect(Screen.width/2, Screen.height*0.2f, 150, 50), "$" + Money);
GUI.Label( new Rect(Screen.width*0.1, Screen.height*0.2f, 150, 50), Depth + " ft.");