Success,Fail Audio not working

I’ve tried the exact same thing done in the lecture but the audio clips are still not playing
here is my code.
I have also added the audio clips in the inspector window of the player.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour

{

  [SerializeField] float delay=2f;

  [SerializeField] AudioClip success;

  [SerializeField] AudioClip fail;

 

  AudioSource audioSource;

  void Start()

  {

         audioSource=GetComponent<AudioSource>();

  }

void OnCollisionEnter(Collision other)

{

    switch(other.gameObject.tag)

   {

       case "Friendly":

              Debug.Log("Friendly");

              break;

       case "Finish":

             

              NextLevelSequence();

              break;

       default:

             

              StartCrashSequence();

              break;

   }

   void NextLevelSequence()

   {

          audioSource.PlayOneShot(success);

          GetComponent<Movement>().enabled=false;

          Invoke("NextLevel",delay);

   }

   void StartCrashSequence()

   {

          audioSource.PlayOneShot(fail);

          GetComponent<Movement>().enabled=false;

          Invoke("ReloadLevel",delay);

   }

}

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,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Are there any error messages in your console?


See also:

Have you checked to see if you have any errors in the console?

Have you checked to make sure the Collision Handler script is on an object with an AudioSource component?

Have you checked the volume on the AudioSource component?

No,there are no errors in the console
and I don’t see any discrepancies in the codes either.

there are no errors in the console

the audiosource is there attached during the main engine thrust process and the volume is also full.

the Thrust sound is working but these two are not???

Maybe the delay value is too low. Check it during runtime with a Debug.Log. If the audio source does not have enough time, it cannot play the sound.

Solved it,

my bad,I actually made a different script for the thrust audio since i was facing issues with it but after merging it the problem is solved.

Thank you for your response…

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

Privacy & Terms