Is the invincible brick supposed to have the crack sound attached?

If yes, it plays on impact. If not, Unity gives a warning.

Hi @Dimi_Nemt,

I believe this is the code in question;

    /// <summary>
    /// Event handler for collision between the ball and bricks
    /// </summary>
    /// <param name="collision">The Collision2D data associated with this collision</param>
    private void OnCollisionEnter2D(Collision2D collision)
    {
        AudioSource.PlayClipAtPoint(crack, this.transform.position, 0.8f);

        if (isBreakable)
        {
            HandleHits();
        }
    }

As you can see there is a test for isBreakable, you could just move the line that plays the clip into this test, e.g.

    /// <summary>
    /// Event handler for collision between the ball and bricks
    /// </summary>
    /// <param name="collision">The Collision2D data associated with this collision</param>
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (isBreakable)
        {
            AudioSource.PlayClipAtPoint(crack, this.transform.position, 0.8f);

            HandleHits();
        }
    }

Hope this helps :slight_smile:

Turns out it was easy. I should have experimented a bit more. Thanks a lot!

1 Like

hehe, there will be many ways to achieve these kinds of things. To answer you original question, you could have removed the crack sound clip from the invincible brick, but as you saw you get an error because the same code is used for all brick types and it assumes it will be there.

An alternative may have been to have create a separate script for the invincible bricks an have that on the prefab for that brick type, but with such a small change, it is less overhead to make this minor change in code.

If you were to start including a lot of different types of bricks which all had unique behaviours, that may be the time to consider having a base class for the bricks which contains the basic functionality that they all share, then, have a separate script which provides each bricks individual unique behaviours for each of the brick prefabs. This would reduce the amount of if statements you may then need to create in order to determine which type of brick it is, and would invariably increase performance fractionally too. :slight_smile:

1 Like