Unable to count how many blocks are destroyed.

Hello! I’m working on a small Arkanoid clone and I’ve run into a little problem, that I’ve been stuck for a day. I’m semi-new to Unity, so I apologize in advance if there’s a simple solution to all of this. I am trying count how many blocks have been destroyed. I’ve set an int (count) to check the times the ball connects with the brick, and that seems to work. However, I’ve also set an int (brickCount) to try and increment the bricks being destroyed, but it never goes past one. I am only using one script, so that might be the problem? Here’s my code:

using UnityEngine;
using System.Collections;

public class Bricks : MonoBehaviour {

    int count = 0;
    int brickCount = 0;

    void OnCollisionEnter2D(Collision2D other)
    {
        count++;
        Debug.Log(count);
        if (count >= 2)
        {
            Destroy(gameObject);
            brickCount++;
            Debug.Log("Destroyed " + brickCount + " brick.");
        }
    }
}

Thank you in advance!

So I’m assuming this script is on each brick. That means that when the brick is destroyed this script is also being destroyed on that brick & since the script is storing the count it will be destroyed as well. You should probably try using an empty game object to store a manager script that you can use to store the brick count. When you destroy the brick pass the brickcount increment across to the manager script.

1 Like

Oh yes, the script is on each brick. I thought I had forgotten to mention something… lol. That’s also a fantastic idea! Knew I was doing something stupid. Thank ya very much :slight_smile: