- Home /
how to keep track of bricks broken
Hi I am currently working on a brick breaking game and need to keep track of when the ball breaks a brick. I was just using a counter that went up everytime it collided and added a script to -1 to the counter every time it hit an object that wasnt a brick. i understand that is not the best way especially cause it wont work. so i was hoping someone here knows how to. Also i am using C#.
Answer by fafase · Aug 12, 2012 at 05:58 PM
How are you brick instantiated?
If you place them manually, then you know how many of them there are.
Create a static variable counter with that value that is decremented when you hit a brick.
If you instantiate them in a loop (double loop probably) in the start, then add a counter++ in each loop and decrement counter each time you hit a brick.
You can also use that variable to check if the scene is empty.
As I said, you know how many of them there is so you can do:
static var counter:int;
function Start(){
counter = numberOfBricks; }
numberOfBricks is a value you give.
Other possibilities is to tag the bricks as Bricks and use a function.
static var counter:int;
function Start(){
var brickArr:GameObject[];
brickArr = GameObject.FindGameObjectsWithTags("Brick");
counter = brickArr.Length,}
I placed them manually and the problem is how do i get it to read that its a brick so it doesnt count the border or the platform.
Your answer
Follow this Question
Related Questions
Scoring (i'm stuck) 3 Answers
Most efficient way to get scores 1 Answer
Make a custom score counter in unity with c# 1 Answer
Score count increase on hit 2 Answers
Collision with cube isn't working when Instantiate clone it 2 Answers