Trying to add sound effect upon contact with Launch Pad but it doesn't work!

Hi! I’ve been trying to add a “thud” sound effect to my rocket whenever it lands on the Launch Pad, but I can’t seem to get it to work. Below is my code and I tried to follow closely what has been done with the “success” and “crash” sound effects as shown in the video. Please help!

using UnityEngine;

using UnityEngine.SceneManagement;

public class CollisionManager : MonoBehaviour

{

[SerializeField] float levelLoadDelay = 2f;

[SerializeField] AudioClip success;

[SerializeField] AudioClip crash;

[SerializeField] AudioClip thud;

AudioSource audio;  

bool isTransitioning = false;

private void Start()

{

    audio = GetComponent<AudioSource>();    

}

void OnCollisionEnter(Collision other)

{

    if(isTransitioning)

    {

        return;

    }

    switch (other.gameObject.tag)

    {

        case "Friendly":

            Debug.Log("This object is friendly.");

            ThudSound();

            break;

        case "Finish":

            Debug.Log("Congratulations! You've finished this level!");

            StartNextLevel();

            break;

        default:

            Debug.Log("Wasted!");

            StartCrashSequence();

            break;

    }

}

void ThudSound()

{

    isTransitioning = false;

    // To do: Add particle effect upon crashing

    GetComponent<Movement>().enabled = true;

    audio.PlayOneShot(thud);

}

void StartNextLevel()

{

    isTransitioning = true;

    audio.Stop();

    // To do: Add particle effect upon crashing

    GetComponent<Movement>().enabled = false;

    Invoke("NextLevel", levelLoadDelay);

    audio.PlayOneShot(success);

}

void StartCrashSequence()

{

    isTransitioning = true;

    audio.Stop();

    // To do: Add particle effect upon crashing

    GetComponent<Movement>().enabled = false;

    Invoke("ReloadLevel", levelLoadDelay);

    audio.PlayOneShot(crash);

}

void ReloadLevel()

{

    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

    SceneManager.LoadScene(currentSceneIndex);

}

void NextLevel()

{

    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

    int nextSceneIndex = currentSceneIndex + 1;

    if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)

    {

        nextSceneIndex = 0;

    }

    SceneManager.LoadScene(nextSceneIndex);

}

}

Hi Lucas,

Welcome to our community! It’s great to see that you are challenging yourself. :slight_smile:

First of all, try to figure out if your ThudSound() method gets called. I assume that is the method which is supposed to play your “thud” sound. Log a message into your console.

Once you ensured that the ThudSound() method gets executed as expected, check if your audio source gets stopped somewhere in your code. Add another Debug.Log there to see if the audio source gets stopped immediately after you played the “thud” sound.

If the code does stop the audio source, I would suggest a simple solution: Create a new audio source just for your “thud” sound. Of course, this new audio source is not supposed to get stopped by your code (unless you come up with a new idea which requires it to be stopped at some point).

I hope this helped so far. Let me know if you managed to make your idea work. :slight_smile:


See also:

Hi Nina,

Thank you for the quick reply. I couldn’t resolve the issue in the CollisionManager script, so I created a new audio source for the Launch Pad and tagged the Rocket to make the “thud” sound as it lands on the launch pad. It works now, but I’m still not sure what was stopping this sound from being played, previously.

Good job on solving the problem. :slight_smile:

Regarding the code, I can see that audio.Stop(); gets executed in StartCrashSequence(). Just by staring at your code, it is impossible to tell whether the problem is in that method, or maybe ThudSound() does not get called at all. Debug.Logs could help you analyse the execution order at runtime.

Privacy & Terms