I'm trying to play a sound on death, but keep getting a null error. I know it is something in my code, and I have already tried looking at an alternate question.
This is my code:
public class BallHealth : MonoBehaviour
{
public float maxPositionY = -10.0f;
void Update ()
{
float positionBallY = transform.position.y;
if (positionBallY <= maxPositionY)
{
StartCoroutine (RestartLevel ());
}
}
IEnumerator RestartLevel()
{
AudioSource audio = new AudioSource ();
audio.clip = (AudioClip)Resources.Load("GameOver") as AudioClip;
audio.Play();
yield return new WaitForSeconds(audio.clip.length);
Debug.Log("You Died....");
SceneManager.LoadScene("LevelOne");
}
}
I already locate my file, in the resource folder.
How do I fix the null error?
