The audio doesn't work on collide? here is an alternative solution

So I followed the lecture step by step, and I made sure that the Audio Listener is enabled on the main camera and removed from the canvas, but the only way I can get the audio to work is by activating Play On Awake, other than that, it doesn’t work on collide.

Here is the screenshot of the code

image

Here is the audio source for the ball

and the main camera

So, After doing some research, I finally found a Solution, and I will share it in this post so it may help you in the future if you ran into the same issue as I did.

First, create an AudioSource in the public class and give it a name

Like this

image

Then replace the the the line of code GetComponent().Play(); with the following code:
name of the audio source = GetComponent();
name of the audio source.Play();

it should look like this :

And it should work just fine.

It does not play the sound with your solution.
I even tried gameObject.GetComponent(); but that does not work either.

I figured out the problem.
I previously created the method as OnCollisionEnter() instead of OnCollisionEnter2D(), then changed it to add 2D in the method name.
However, when I commented out the method and created a new method OnCollisionEnter2D() and copied the GameComponent().Play() into the new method, it started playing the sound.
This might be a bug.

I had the same problem as you, thanks very much! What a weird bug.

Privacy & Terms