- Home /
How can i link a var to a gui?
Hi i need to know how to use a variable in a script and copy that variable in a gui script. In other words i need to know how i can link the var to a gui text.
Answer by OperationDogBird · Aug 21, 2012 at 10:59 PM
You would reference that variable in the line for the string.
SomeScript
public var valueVariable:float;
GUIScript
private var someScript:SomeScript;
function Start()
{
someScript=transform.GetComponent(SomeScript);
}
function OnGUI()
{
GUI.Label(rect,someScript.valueVariable.ToString("f2"));
}
Thanks but it says: $$anonymous$$ identifier `f2´ How can i fix that and what does it mean
He ment .ToString("f2")
You can even omit the format string and just use .ToString()
GUI.Label(rect,someScript.valueVariable.ToString());
Sorry bout that, forgot the quotes. Thanks @Bunny83 for correcting that.
@patben8 The "f2" format makes the output string display 2 characters past the decimal rather than the entire value. Its useful if you display float values and only want the short version. "1.25621" will be displayed as "1.25". You could also use "f1" to display 1 digit past the decimal. If it is an int, it will add a decimal and 0's. With "f1" "3000" becomes "3000.0"
Answer by patben8 · Aug 24, 2012 at 05:33 PM
ok thanks but it gives me this
BCE0023: No appropriate version of 'UnityEngine.GUI.Label' for the argument list '(System.Type, String)' was found.
OH And thanks so much for the help guys!
Your answer
Follow this Question
Related Questions
Assigning two variables ? 1 Answer
GUI after pressed Destruction 2 Answers
Two variables in a GUI Button? 1 Answer
GUI.TextField problem, won't update var. 0 Answers