- Home /
How to Display only current instead of current/max on a GUI.Box
I have trouble figuring out what to put here,I originally wanted to display current/max but now i wanted to change that to only display the current in the box,I tried for a bit and had to put a placeholder where the maxhealth variable was to prevent error.
public float health = 100.0f;
public float maxHealth = 100.0f;
GUI.Box(new Rect(10, 20, healthbarLength, 20), health + "/" + "MaxHealth");
i tried this but it got an error
GUI.Box(new Rect(10, 20, healthbarLength, 20), health );
here's the error (Assets/Weapons/Scripts/Player.cs(53,13): error CS1502: The best overloaded method match for `UnityEngine.GUI.Box(UnityEngine.Rect, string)' has some invalid arguments
Answer by rutter · Jan 23, 2014 at 03:29 AM
C# is a strongly typed programming language. Numbers and strings aren't equivalent.
In some languages, a variable can be used as both number and string.
In some languages, a variable which is a number can be automatically converted to a string.
In some languages, a variable which is a number can be explicitly converted to a string.
C# does a little bit of those last two. An operation like this causes an implicit conversion:
int x = 5;
int y = 10;
string s = x + "/" + y;
An operation like this causes an explicit conversion:
int x = 5;
string s = x.ToString();
As to fixing your error, you just need that conversion:
GUI.Box(new Rect(10, 20, healthbarLength, 20), health.ToString() );
Your answer
Follow this Question
Related Questions
What is the best way to concatenate string to create an interaction logging console? 4 Answers
How do I change the fount size of GUI 1 Answer
Make String Update for Value (GUI) 1 Answer
Display additional text before the variable that the user is editing in a GUI Text Field 1 Answer
Changing a GUI String to read as a INT 2 Answers