PlayOneShot was called with a null AudioClip

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);

}

}

2 Likes

Oh, nevermind, I kept going with the course and I had the same issue with the duplicated script >.<
Will keep the post in case someone runs into the same issue (I lost a good hour searching on google before writing this)

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

Privacy & Terms