What is wrong with my code? NullReferenceException

okay so the code is to play a audio clip once the “ball”/player reaches y-10. but now instead of restarting the level at y-10 the ball keeps falling and i get the error message

NullReferenceException: Object reference not set to an instance of an object
BallHealth+$RestartLevel$6+$.MoveNext () (at Assets/Scripts/BallHealth.js:24)
UnityEngine.MonoBehaviour:StartCoroutine_Auto(IEnumerator)
BallHealth:Update() (at Assets/Scripts/BallHealth.js:14)

This is my code:

 #pragma strict

var maxFallDistance = -10;
private var isRestarting = false;

var GameOverSound : AudioClip;

function Update ()
{
    if (transform.position.y <= maxFallDistance)
    {
        if (isRestarting == false)
        {
            RestartLevel();
        }
    }
}

function RestartLevel () {
    isRestarting = true;
    GetComponent.<AudioSource>().pitch = 1;
    GetComponent.<AudioSource>().clip = GameOverSound;
    GetComponent.<AudioSource>().Play();
    yield WaitForSeconds (GetComponent.<AudioSource>().clip.length);
    Application.LoadLevel("Level01");
}
[\Code]

It’s either complaining that there isn’t an AudioSource component attached to the GameObject, or there is no clip in the AudioSource.

1 Like