I get this error when I hit the obstacle or the landing pad

NullReferenceException: Object reference not set to an instance of an object
CollisionHandler.StartCrashSequence () (at Assets/Scripts/CollisionHandler.cs:45)
CollisionHandler.OnCollisionEnter (UnityEngine.Collision other) (at Assets/Scripts/CollisionHandler.cs:30)

This is my script
using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour

{

[SerializeField] float levelLoadDelay = 2f;

[SerializeField] AudioClip success;

[SerializeField] AudioClip crash;

AudioSource Audio;

void start()

{

    Audio=GetComponent<AudioSource>();

}

void OnCollisionEnter(Collision other)

{

     switch (other.gameObject.tag)

     {

         case "Friendly":

            Debug.Log("This place is friendly");

            break;

        case "Finish":

            StartSuccessSequence();

            break;

        default:

            StartCrashSequence();

        break;

     }

}

void StartSuccessSequence()

{

    Audio.PlayOneShot(success);

    GetComponent<Movement>().enabled = false;

    Invoke("LoadNextLevel", levelLoadDelay);

}

void StartCrashSequence()

{

    Audio.PlayOneShot(crash);

    GetComponent<Movement>().enabled = false;

    Invoke("ReloadLevel", levelLoadDelay);

}

void LoadNextLevel()

{

    int currentscene = SceneManager.GetActiveScene().buildIndex;

    int NextScene=currentscene+1;

    if(NextScene==SceneManager.sceneCountInBuildSettings)

    {

        NextScene=0;

    }

    SceneManager.LoadScene(NextScene);

}

void ReloadLevel()

{

    int currentscene=SceneManager.GetActiveScene().buildIndex;

    SceneManager.LoadScene(currentscene);

}

}

Hi,

Welcome to our community! :slight_smile:

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:

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

Privacy & Terms