- Home /
Shot Counter Question
So I have been trying for ages now on what should be a perfectly simple script. All I need to it do is add 1 to a number on the GUI when i press a button. Except it just won't work. And yes I've searched but everything I have found has been for something different or for a much more complicated function.
So far I have
public var ShotsFired : String = "0";
function OnGUI() { GUI.Label(Rect(100, 100, 140, 20), ShotsFired); }
function Update () { if (Input.GetButtonDown("Fire1")) { print ("you clicked the icon"); ShotsFired += 1; } }
Answer by Eric5h5 · Feb 24, 2010 at 05:25 PM
You can't treat strings as if they are numbers; you must use a number and convert it to a string as necessary.
var shotsFired = 0;
function OnGUI() { GUI.Label(Rect(100, 100, 140, 20), shotsFired.ToString()); }
function Update () { if (Input.GetButtonDown("Fire1")) { print ("you clicked the icon"); shotsFired++; } }
Brilliant, I thought it had something to do with changing it to an integer but whenever I tried I just got errors.
Answer by Ehren · Feb 24, 2010 at 05:25 PM
I assume the button press is being captured correctly. If that's the case, try changing ShotsFired to an integer instead of a String.
public var ShotsFired : int = 0;
function OnGUI() { GUI.Label(Rect(100, 100, 140, 20), ShotsFired.ToString()); }
function Update () { if (Input.GetButtonDown("Fire1")) { print ("you clicked the icon"); ShotsFired += 1; } }
What currently happens? Does it print out 1, then 11, then 111, etc.?