Nothing plays after the "audioSource.Stop" step

(Complete unity course - Project Boost - Bool Variable For State) I’ve been like 1h with this problem,i am at the step where you implement the “audioSource.Stop” step to stop the engine sound playing,and no sound plays after i do it,also the “istransitioning {return}” didn’t work until i’ve put it outside the switch,here is my code and thanks for the help
{
using UnityEngine;
using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour
{
[SerializeField] float levelLoadDelay = 2f;
[SerializeField] AudioClip explosion;
[SerializeField] AudioClip success;

AudioSource audioSource;

bool isTransitioning = false;

void Start()
{
    audioSource = GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision other)
{
    if (isTransitioning) { return; }

    switch (other.gameObject.tag)
    {

        case "Friendly":
            Debug.Log("This thing is friendly");
        break;

        case "Finish":
            StartSuccessSequence();
        break;

        default:
            StartCrashSequence();
        break;

    }
}

void StartSuccessSequence()
{
    isTransitioning = true;
    audioSource.Stop();
    audioSource.PlayOneShot(success);
    GetComponent<Movement>().enabled = false;
    Invoke("LoadNextLevel", levelLoadDelay);
}

void StartCrashSequence()
{
    isTransitioning = true;
    audioSource.Stop();
    audioSource.PlayOneShot(explosion);
    GetComponent<Movement>().enabled = false;
    Invoke("ReloadLevel", levelLoadDelay);
}
void LoadNextLevel()
{
    int CurrentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    int nextSceneIndex = CurrentSceneIndex + 1;
    if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
    {
        nextSceneIndex = 0;
    }
    SceneManager.LoadScene(nextSceneIndex);

}
void ReloadLevel()
{
    int CurrentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    SceneManager.LoadScene(CurrentSceneIndex);
}

}}

Hi,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

I’m wondering if your script is attached to your rocket that has the AudioSource component? There should be just one AudioSource in your scene right now, attached to the player controlled rocket. Both the movement script and collision handler should use the same audio source. Anyhow, you can add a bit of debugging code to your Start() method to double check your script found the component:

audioSource = GetComponent<AudioSource>();
Debug.Assert(audioSource != null, "No audio source!");
1 Like

Privacy & Terms