- Home /
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'
I ran into that a bit ago here is the code I was working on hope it helps you solve your problem. using UnityEngine; using System.Collections;
public class RPGplayerstats : $$anonymous$$onoBehaviour {
public float curHP = 37;
public int maxHP = 37;
public float curEP = 30;
public int maxEP = 30;
public int capEXP = 150;
public int curEXP = 0;
public int Level = 1;
public int DPS = 3;
void OnGUI () {
GUI.Box(new Rect (5, 5, Screen.width / 5 / (maxHP / curHP), 20), curHP + "/" + maxHP);
GUI.Box(new Rect (5, 15, Screen.width / 5 / (maxEP / curEP), 20), curEP + "/" + maxEP);
GUI.Box(new Rect (5, 30, Screen.width / 5 / (capEXP / curEXP), 20), curEXP + "/" + capEXP);
}
void update (){
if(curEXP >= capEXP)
{
Level = Level + 1;
}
if(curHP < 0){
curHP = 0;
}
if(curHP > maxHP){
curHP = maxHP;
}
if(curEP < 0){
curEP = 0;
}
if(curEP > maxEP){
curEP = maxEP;
}
}
}
I ran into the problem when I tried to display the current level
I also don't know what screen.height will do for you but that might be causing you problems. I really don't know much about gui
Answer by perchik · Sep 09, 2013 at 02:42 AM
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.");
Answer by ZouBi27 · Sep 09, 2013 at 03:06 AM
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.
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 answer
Follow this Question
Related Questions
GUI Text Blinking came up with one error. 3 Answers
Translating from game space to view space 2 Answers
Gui label variable as a string.. 1 Answer
Tracking Down GUI Errors 0 Answers
Can't use any GUI functions 1 Answer