- Home /
Whats wrong with my script? GUI C#
I have GUI coding here and for the life of me i cant figure out where my errors are.
Here is my script
void OnGUI() {
GUI.Label (new Rect (25, 25, 100, 30), "Job:");
GUI.Label (new Rect (10, 10, 100, 30), curJob);
GUI.Label (new Rect (30, 10, 100, 30), "Salary:");
GUI.Label (new Rect (60, 10, 100, 30), Salary);
GUI.Label (new Rect (110, 10, 100, 30), "Money");
GUI.Label (new Rect (150, 10, 100, 30), curMoney);
I do have these variables
public int curMoney = 1000;
public int curHealth = 100;
public int maxHealth = 100;
public string curJob = "Citizen";
public float Salary = 50f;
I am probably barely missing where the problem is and Im reading these errors and change stuff and still i get errors...
Assets/Scripts/JobInfo.cs(26,21): error CS1502: The best overloaded method match for `UnityEngine.GUI.Label(UnityEngine.Rect, string)' has some invalid arguments
Assets/Scripts/JobInfo.cs(26,21): error CS1503: Argument #2' cannot convert
float' expression to type string' Assets/Scripts/JobInfo.cs(28,21): error CS1502: The best overloaded method match for
UnityEngine.GUI.Label(UnityEngine.Rect, string)' has some invalid arguments
Assets/Scripts/JobInfo.cs(28,21): error CS1503: Argument
#2' cannot convert
int' expression to type `string'
Answer by Peter G · Dec 30, 2010 at 05:03 PM
For curMoney and Salery you need a string instead of a float. Fortunately, Mono has a function for that:
void OnGUI() {
GUI.Label (new Rect (25, 25, 100, 30), "Job:");
GUI.Label (new Rect (10, 10, 100, 30), curJob);
//You need to change your float to a string in the 2nd of these 2
GUI.Label (new Rect (30, 10, 100, 30), "Salary:");
GUI.Label (new Rect (60, 10, 100, 30), Salary.ToString());
//and here--------------------------------------------------
GUI.Label (new Rect (110, 10, 100, 30), "Money");
GUI.Label (new Rect (150, 10, 100, 30), curMoney.ToString());
}
thanks man so much... that was a quick respond i literally posted this 30 1 $$anonymous$$ute before u responded.
Answer by Bunny83 · Dec 30, 2010 at 06:11 PM
If you don't like to call ToString() manually you can do:
GUI.Label (new Rect (60, 10, 100, 30), "" + Salary);
that implicit calls ToString() because if you use the +operator on a string C# tries to convert the second opperand to string.