I’m having two issues, the first is just a warning I got after setting up the crash and winning clips that says “PlayOneShot was called with a null AudioClip” but the crash audio and the winning audio are being played properly so it didn’t really bother me.
But now after setting the audiosource to stop during the transition no audio is being played, not even the clips.
Here is my code:
using UnityEngine;
using UnityEngine.SceneManagement;
public class ColisionHandler : MonoBehaviour
{
[SerializeField] AudioClip crash; [SerializeField] AudioClip winMusic; [SerializeField] AudioSource audioSource; [SerializeField] float delayLevel = 1f; bool isTransitioning = false; private void Start() { audioSource = GetComponent<AudioSource>(); } void OnCollisionEnter(Collision other) { if (isTransitioning) { return; } switch(other.gameObject.tag) { case "Friendly": Debug.Log ("Has chocado con algo amistoso"); break; case "Finish": Debug.Log("Has llegado a la meta"); NextLevelSequence(); break; default: Debug.Log("Te has morido"); StartCrashSequence(); break; } } void StartCrashSequence() { isTransitioning = true; audioSource.Stop(); audioSource.PlayOneShot(crash); GetComponent<Movement>().enabled = false; Invoke ("ReloadLevel", delayLevel); } void NextLevelSequence() { isTransitioning = true; audioSource.Stop(); audioSource.PlayOneShot(winMusic); GetComponent<Movement>().enabled = false; Invoke ("LoadNextLevel", delayLevel); } 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); }
}