- Home /
GUI text label using variable values and text
Hey all, just a quickie.
i have a score collector that 'collects' through out each level scores, achievements etc.
it is stored until the last scene were the GUI will display all this;
my questions is how do i make my labels or text display and do math functions, like... you see: "5 power ups X 100 points = 500"
var Collector: GameObject;
function Start () { Collector = GameObject.Find("Collector"); }
And on the function OnGui i'd like to do this;
GUI.Label (Rect (10, 10, 100, 20), "Tackled '+tackledamount' X DO MULTIPLY by 100 = "answer" ");
... yeh that looks terrible.. but i want it to end up on screen as;
"Tackled X times 100 points = X"
... and the values i want to grab from the script "Collector"... so i dont know how to take those value and use them in this GUI script or how to do the math on them on the gui?
Matty
Answer by Jake-L · Mar 28, 2011 at 10:05 AM
Use the right syntax and it should work as you want it:
GUI.Label(Rect(10,10,100,20),"Tackled "+Collector.TackledAmount+" times 100 points = "+Collector.TackledAmount*100);
thanks for your help, i didnt know the syntax to use, so thank you for showing me.
I tried the code... i get "Collector is not a member of Unity.GameObject" .. i change the variable to Object... i get a huge load of Boo errors...
What am i ment to set the variable as?
$$anonymous$$atty
You made your assignment wrong. If you just do "Collector = ..." then it assumes that Collector is some sort of member of Unity script. You need to do something like "GameObject collector = ..." and it will work.
I don't understand.. The variable is stated as a GameObject, Collector being its name, then in the Start function it assigns it as such, thats all fine... I have found a new problem though, Collector is the GameObject, Tackled amount isnt a component.. so it would need to be Collector.Collector.TackledAmount... ??? what!?
I just corrected your syntax in the GUI.Label call. TackledAmount and any other variable you declare can't be part of an gameobject. They must be declared inside a component that you attach to a gameobject, I thought that was obvious. So you'll need to find your script using GameObject.Find("Collector").GetComponent("$$anonymous$$yCollectorClass");
my issue was a spelling mistake... laugh at me please. All of your ideas would have worked fine... i'm ashamed. Thanks everyone, glad to close this question.
Your answer
Follow this Question
Related Questions
Optimizing OnGUI 1 Answer
OnGUI called after LateUpdate screwing up debug text database 1 Answer
IF in a GUI 2 Answers
Can't draw GUI.Label text on subpixel values 0 Answers
Inputfield set as Variable 1 Answer