Crack sound doesn't play when i hit the brick also boing sound plays everytime even when i hit the brick

Hey
i tried to shut downt boing sound by this in ball script
if (hasStarted && collision.gameObject.tag != “breakable”)
bt it makes no difference also my crack sound doesnt play
thanks for feedback

Hi,

For ball sound you did a good thing :

AudioSource audio = GetComponent<AudioSource> ();
audio.Play();

But for your brick you change the way you did it. (In fact, I’ve try to do something similar once … I’ve preferred to go back to the first method).
I think that the playclipatpoint is only valuable for 3D game with sound positioning (and I’m not totally sure of that).
You should stick with the GetComponent() Method like you did on your ball.

Just to add, ClipPlayAtPoint is useful because it instantiates and disposes of an AudioSource. The reason this is useful in BlockBreaker is because if you rely on a component of the brick, once the brick is destroyed, the AudioSource component would be also - and then you will run into null reference exception errors.

The point in space will work happily in a 2D environment just as a 3D environment.

Judging by the error message I would guess that maybe the crack audio clip hasn’t been dragged into the Crack field you expose on line 6 of the Brick class within the Inspector?

Note, for code, it’s much easier to just copy and paste your code in, see the note below regarding formatting it.


See also;

1 Like

the sollution from @Dieedi works but crack sound doesnt activate when brick is destroyed

i dont entirely understand @Rob answer can you write me the whole line please?

The crack sound wouldn’t play when the brick is destroyed because your AudioSource is a component of the Brick game object. When you destroy the Brick, the game object is destroyed, that includes all of it’s components, e.g. the Audio Source. It can’t play because it doesn’t exist.

The PlayClipAtPoint method creates it’s own Audio Source object, uses it, then destroys it after the clip has played, as such, you don’t need to worry about the game object, in this case the Brick, being destroyed and not playing the sound.

Make sense?

i understand but crack sound doesn’t doesnt activate when i hit the brick with that PlayClipAtPoint method can you tell me how the line should look like?

Did you check the link to the documentation I gave you?

Here’s an extract;

    public AudioClip clip;

    void Start() {
        AudioSource.PlayClipAtPoint(clip, new Vector3(5, 1, 2));
    }

In the above, clip is your audio clip that you are passing it via the Inspector.

AudioSource is the class.

PlayClipAtPoint is the method, it’s what’s called a static method, e.g. you don’t need to instantiate a new AudioSource object in order to use it.

Within this method call, you pass in the clip, a location to play the sound as Vector3 (I believe a Vector2 will work also as it’s simply a Vector3 without the Z position), and optionally, the volume as a float - if you don’t specify the volume it will default to 1.0f.

Yes @Rob, it make sense. Thanks for the explanation. I’ve been struggling with why the AudioSource didn’t play immediately after the a brick got destroyed.

I also refined it by detecting based on collision objects in each OnCollisionEnter2D functions for each prefabs to check which audio it plays whether it collides with “Ball” or “Paddle”.

1 Like

Glad to hear this was of use and you’re welcome. :slight_smile:

Privacy & Terms