My crash sound is not playing - I'm starting to lose my mind :(

Hello everyone! :slight_smile:

So my beautiful rocket is playing thrusting and finish sound without any problem but when I try to get it to play crash sound it’s not working at all, here is my code:

using System;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ColisionHandler : MonoBehaviour
{

    [SerializeField] AudioClip finishSound;
    [SerializeField] AudioClip crashSound;
    [SerializeField] float delayTime = 1f;

    AudioSource audioSource;

    const string FRIENDLY = "Friendly";
    const string FINISH = "Finish";

    private void Start()
    {
        audioSource = GetComponent<AudioSource>();
    }

    private void OnCollisionEnter(Collision collision)
    {
        switch (collision.gameObject.tag)
        {
            case FRIENDLY:
                Debug.Log("Friendly");
                break;
            case FINISH:
                Debug.Log("Finish");
                StartSuccessSequence();
                break;
            default:
                Debug.Log("Default");
                StartCrashSequence();
                break;
        }
    }

    private void StartSuccessSequence()
    {
        audioSource.PlayOneShot(finishSound);
        //TODO add particle effect upon crash
        GetComponent<Movement>().enabled = false;
        Invoke("LoadNextLevel", delayTime);
    }

    private void StartCrashSequence()
    {
        Debug.Log("Should play crash sound");
        audioSource.PlayOneShot(crashSound);
        //TODO add particle effect upon crash
        GetComponent<Movement>().enabled = false;
        GetComponent<AudioSource>().enabled = false;
        Invoke("ReloadLevel", delayTime);
    }

    private void ReloadLevel()
    {
        SceneManager.LoadScene(GetCurrentSceneIndex());
    }

    private void LoadNextLevel()
    {
        int nextSceneIndex = GetCurrentSceneIndex() + 1;
        if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
        {
            nextSceneIndex = 0;
        }
        SceneManager.LoadScene(nextSceneIndex);
    }

    private int GetCurrentSceneIndex() => SceneManager.GetActiveScene().buildIndex;

}

I tried changing my crashing sound to finish one but still it’s not playing. Also I put the debug logs and everything looks fine, I get the right messages. Idk what’s wrong, and what else I can check, could I kindly ask for any advice?

And ofc. 1 second after I posted this question I found the reason…

GetComponent<AudioSource>().enabled = false;

I have no idea what it’s doing here, who put it there :smiley: and how I didn’t saw it for 30 minutes when I tried to find the source of the problem hahaha :slight_smile:

Sorry for that, have a good day :slight_smile:

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

Privacy & Terms