- Home /
Updating GUI.Label for Score
I am trying to use a GUI.Label to update the score of my game. My problem is I do not know how to get the score to increase on the GUI. I know that label is just used to display information, and I have tried using a string instead, but it didn't work for me. I might have done it wrong so I am asking for help. Here is my script for the text
public var score : int = 0;
function OnGUI () { GUI.Label (Rect (25, 25, 100, 30), "Score:" + score); }
function OnCollisionEnter(collision : Collision) {
//if I collide with a bullet,destroy myself
if(collision.gameObject.tag=="Bullet")
{
score += 1;
Destroy(gameObject);
}
}
EDIT: The GUI shows up and shows a starting score of 0 when I destroy the object in question, the score does not change in the GUI but if I Debog.Log, the score goes up by 1.
I have same problem... any chances that somebody knows something and is willing to share? I can always post another question, but that's not neccesary, I think. In one script (connected with player) I've hit points, exp, etc. and after colliding with box it ads exps taking hps, etc... Problem is that my GUI (shows after key "$$anonymous$$" or "I") is not updating. For me everything is like above besides fact that I'm taking "score" from another script so is going like:
GUI.Label (r_expLabel, currentEXP.ToString(), "Text Amount");
where var currentEXP = playerInfo.actualEXP;
I was trying reimporting and everything.
Answer by _samuel12345 · Mar 29, 2017 at 03:52 PM
I was having the same problem. Don't make the variable "score" a public variable, make it static. So instead of:
public var score : int = 0;
Use:
static var score : int = 0;
Answer by Kleptomaniac · Mar 18, 2012 at 10:30 AM
Instead of:
function OnGUI () {
GUI.Label (Rect (25, 25, 100, 30), "Score:" + score);
}
Try:
function OnGUI () {
GUI.Label (Rect (25, 25, 100, 30), "Score:" + score.ToString());
}
Converting the score int to a string value so it can be displayed in the GUI.Label.
Hope that helps, Klep
Well, I tried to change the score to score.toString() yet it still is not updating itself to the score's new amount. I tried using a debug.log to see if the score is changing, it is changing but the number by the text is not.
Could you try just commenting the Destroy(gameObject) line and running the script like that? I bet you it has something to do with the fact that OnGUI does not have a chance to update before the script is destroyed. That is just speculation of course though ... :P
I tried that... nothings change beside the fact that object (in my case is tester $$anonymous$$r.Cube) is not destroying - best of it that script aplied to player still is counting everything right... only that GUI label. Please help...
Answer by tianjishu · Mar 19, 2012 at 03:42 AM
pragma strict
ar score : int = 0; function OnGUI () { GUI.Label (Rect (25, 25, 100, 30), "Score:" + score); }
I copy your function and running.Your function have not problem!!!!
Its not that my code has problems. $$anonymous$$y score does not update upon destroying the object in question. When I debug.log, the console shows the score going up, but the GUI never changes. its stays saying Score: 0
Answer by gameangel · Feb 03, 2014 at 06:50 PM
My Label does update the score "in the console it says so" but it dosent show up in the game. Help?
Answer by morgan23 · Feb 03, 2014 at 09:25 PM
hmm from what I seen your code should work I have an example I did too test my health in game it's in c# through but should be easy too change. void Update () { if(Input.GetKeyDown(GUIKey)) { Life -=1; } } it update's correct when is pressed my GUI label is in another script but works for what you want.
Your answer