Confused after audio.Play(); didn't work

I’ve searched for the Unity 5.6 code changes but it made me more confused. Everyone seems to do it a little differently.

I’ve replaced audio.Play();

with GetComponent ().Play ();

And I can only get the sound to play once on start. When I move it to

void OncollisionEnter2D (Collision2D collision) {

No sound is played. What am I missing?

49 PM

I’ve added the

AudioSource.PlayClipAtPoint (crack, transform.position);

in the brick script but now my bricks don’t break!

45 AM

One issue here is case-sensitivity in your Blueglassball.cs script.

OncollisionEnter2D is not recognised by Unity because of your lower-case “c”, it should in fact be OnCollisionEnter2D.

Regarding the bricks not breaking, it seems unlikely that just adding the one line for the AudioSource.PlayClipAtPoint would prevent this, which suggest other issues.

I would consider adding a Debug.Log statement on the first line of the OnCollisionEnter2D method and output something of use to the console, this will help you to determine if this method is being called at all, for example;

void OnCollisionEnter2D(Collision2D col)
{
    Debug.Log("OnCollisionEnter2D called in Brick.cs");

    AudioSource.PlayClipAtPoint(crack, transform.position);

    if(isBreakable)
    {
        HandledHits();    // note in the course this is named "HandleHits"
    }
}

Run the game, if you see our message appear in the console then you are sure this method is being called. Assuming it is, I would then add another Debug.Log statement to the first line within the HandledHits method, and then finally within the LoadSprites method.

Assuming all of these appear in the console you can be sure the code is calling the correct methods. After this I would perhaps look at things like, is it just the sprite that isn’t changing, have I added all of the sprites, have I made changes to an instance of a Brick instead of the Brick prefab.

Let me know how you get on with the above and post back any on going issues.

Note, you can just copy/paste your code into your posts which makes it a lot easier to work with, see the link below for how to apply formatting.


See also;

Hey, Rob

One issue here is case-sensitivity in your Blueglassball.cs script.

OncollisionEnter2D is not recognized by Unity because of your lower-case “c”, it should, in fact, be OnCollisionEnter2D.

The sound works now as it should after changing that one character in my Ball script.

As far as my bricks not breaking. I got a new error saying I had to assign the crack sound to my bricks in the inspector. After I did that it all worked.

It seems like it’s always something small that stops progress.

Thank you for your help!
Pan

1 Like

Hi Pan,

Really pleased to hear you are able to move forward again, and you’re more than welcome :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms