Search Unity

What is wrong with my code? NullReferenceException

Discussion in 'Editor & General Support' started by novafluff, Aug 16, 2015.

  1. novafluff

    novafluff

    Joined:
    Aug 16, 2015
    Posts:
    1
    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:

    Code (csharp):
    1.  #pragma strict
    2.  
    3. var maxFallDistance = -10;
    4. private var isRestarting = false;
    5.  
    6. var GameOverSound : AudioClip;
    7.  
    8. function Update ()
    9. {
    10.     if (transform.position.y <= maxFallDistance)
    11.     {
    12.         if (isRestarting == false)
    13.         {
    14.             RestartLevel();
    15.         }
    16.     }
    17. }
    18.  
    19. function RestartLevel () {
    20.     isRestarting = true;
    21.     GetComponent.<AudioSource>().pitch = 1;
    22.     GetComponent.<AudioSource>().clip = GameOverSound;
    23.     GetComponent.<AudioSource>().Play();
    24.     yield WaitForSeconds (GetComponent.<AudioSource>().clip.length);
    25.     Application.LoadLevel("Level01");
    26. }
    27. [\Code]
     
    Last edited: Aug 16, 2015
  2. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    322
    It's either complaining that there isn't an AudioSource component attached to the GameObject, or there is no clip in the AudioSource.
     
    Graham-Dunnett likes this.