Sound issue with adding two sounds on one AudioSource Udemy 2d course

I have taken a screen shot of my code and inspector. The rail (BoardGrind) sound works but the ground (BoardPowder) sound does not. What am I missing? In the lesson he just added one sound but stated that was the way you could add more sounds. Also, my particle effect works on the script. No exceptions are thrown. I have tried adding another Audio Source but that didn’t work either. When I comment out the rail sounds the powder sounds work. Thanks for any help.


“GroundSound” doesn’t work because you are stopping the playback immediately.

other.gameObject.tag is equal to “Ground”. So, you start playing the sound. All good. BUT
other.gameObject.tag is not equal to “Rail”. So, you stop the sound.

Try this instead

if (other.gameObject.tag == "Ground")
{
    GetComponent<AudioSource>().PlayOneShot(GroundSound);
    boardParticles.Play();
}
else if (other.gameObject.tag == "Rail")
{
    GetComponent<AudioSource>().PlayOneShot(GroundGrind);
}
else
{
    GetComponent<AudioSource>().Stop();
}
1 Like

I was bashing my head against a wall trying to figure this out. But that makes sense now. Since Ground isn’t Rail with my code it was always going to do the else statement every time I hit the ground. This helps me understand the code a bit better. Thanks.

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

Privacy & Terms