- Home /
Cube Counter
I have a game where cubes are chasing me. When I touch the cubes they die. I would like a GUI counter telling me how many cubes are left.
I don't have any code yet.
Any code would be appreciated...
All you need is a variable that contains the count and just subtract from it when you destroy 1 cube.
var int : count = .. void OnGUI(){ GUI.Box (new Rect (0,0,100,50), count); } //and somewhere else a function or if.. if(cubeDestroyed){ count -= 1; }
Have a look at http://docs.unity3d.com/Documentation/Components/gui-Basics.html
Answer by roojerry · Sep 02, 2013 at 11:45 AM
the simple but inefficient way would be:
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), GameObject.FindGameObjectsWithTag("Cube").Length.ToString());
}
ideally you don't want to call a Find every frame, so @ShadoX 's solution would be preferable, you would just need to handle a few more things to keep track of when Cubes were being destroyed to decrement a variable
THAN$$anonymous$$YOU SO $$anonymous$$UCH! THIS WOR$$anonymous$$ED FIRST TI$$anonymous$$E!
Your answer
![](https://koobas.hobune.stream/wayback/20220613113426im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Clearing a GUI and Pausing a Game 1 Answer
HighScore analytics 0 Answers
Add a Prefix to a Variable 1 Answer
Get Mouse Position GUI 3 Answers
Array Problem - Error Code BCE0022 1 Answer