Dead and finish sound don't play

Hi
I have added the two AudioClips to the Collision script with plus the audio source.
But it won’t play when I hit an object. When I hit an object or get to the finish i get this error.
Schermafbeelding 2021-06-01 160952

Here is my code


public class CollisionChecker : MonoBehaviour
{   
    
     //PAREMTERS 
     [SerializeField] float LevelLoadDelay = 2f;
     

      public AudioClip DeadSoundPlay;
      public AudioClip YouMadeIt;

      // CACHE 
      AudioSource audioSource;


 void start() 
 {
      audioSource = GetComponent<AudioSource>();
 }
     void OnCollisionEnter(Collision other) 
     {
       switch (other.gameObject.tag)
       {
           case "Friendly":
                Debug.Log("this thing is nice");
                break;
            case "Finish":
               StartSuccesSequenceNOW();

                break;    
            
            default:

                StartCrashOfShip();
                 
                
                 break;         
       }
     }
    //When you win Go to next level
    private void StartSuccesSequenceNOW()
    {   
        audioSource.PlayOneShot(YouMadeIt);
        // totdo add particals
        GetComponent<Movment>().enabled = false;
        Invoke("LoadTheNextlevel",LevelLoadDelay);
    }
    
    //If you hit somthing reload level. 
    void StartCrashOfShip()
     {
        
        GetComponent<Movment>().enabled = false;
        Invoke ("ReloadLevel", LevelLoadDelay);
        // totdo add particals
        audioSource.PlayOneShot(DeadSoundPlay);
     }

     void LoadTheNextlevel()
     {
         int currentLevelIndex = SceneManager.GetActiveScene().buildIndex;
         int nextLevelIndex = currentLevelIndex + 1;
         if (nextLevelIndex == SceneManager.sceneCountInBuildSettings)
         {
             nextLevelIndex = 0;
         }
         SceneManager.LoadScene(nextLevelIndex);

        SceneManager.LoadScene(currentLevelIndex + 1);
     }


    void ReloadLevel() 
    {
        int currentLevelIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentLevelIndex);
    }  
     

 }


Hi,

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.


See also:

He is pointing to these to lines

    private void StartSuccesSequenceNOW()
    {   //this one 
        audioSource.PlayOneShot(YouMadeIt);
        // totdo add particals
        GetComponent<Movment>().enabled = false;
        Invoke("LoadTheNextlevel",LevelLoadDelay);
    }
    
    //If you hit somthing reload level. 
    void StartCrashOfShip()
     {
        
        GetComponent<Movment>().enabled = false;
        Invoke ("ReloadLevel", LevelLoadDelay);
        // totdo add particals

        //and this one also 
        audioSource.PlayOneShot(DeadSoundPlay);
     }

But the fields are not empty in the inspector
inspector

Do the fields become empty during runtime when you get the NullReferenceException?

No they stay there not being removed from the inspector slots while I get the error.

Oke I got it working it! Was because I forgot to give void start a capital S :sweat_smile:

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

Privacy & Terms