NullReferenceException : Objectt refernce not set to an instance of an object

im unable to fix this error on unity where the colliders for my rocket stop working and the scenes dont advance or reset after implementing the audiosource into the collider script i can send the link to the script if you know a solution to this problem many thanks

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CollisionBehaviour : MonoBehaviour
{
    [SerializeField] float LevelLoadDelayTime = 2f; // member level variable which holds the delay time of whent he next scene should start
    [SerializeField] AudioClip Success;
    [SerializeField] AudioClip Crashed;
    AudioSource audioSource; 

    void start()
    {
        audioSource = GetComponent<AudioSource>(); // cached variable is formed here
    }

    void OnCollisionEnter(Collision other) // when other (the player) collides with a boxcollider
    {
       switch (other.gameObject.tag) // switch operator "the if statement subsitute for tags"
       {
            case "Friendly": // checks the tags 
                Debug.Log("This is friendly"); 
                break; // this is when it should end
            case "Finish":
                SuccessSequenceActive();
                break;
            default:
                YouCrashed(); // new method for playing the crash sequence
                break;
       }
    }
    void SuccessSequenceActive() // new method for when the player successfully reaches the other platform
    {
        audioSource.PlayOneShot(Success);
         // finds the cached variable and plays the success tune 
        GetComponent<RocketMovement>().enabled = false; // deactivates script for movement
        Invoke("LoadNextLevel" , LevelLoadDelayTime); // delays the loadnextlevel method through the variable LevelLoadDelayTime
    }
    void YouCrashed() // new method for when the player crashes
    {
        audioSource.PlayOneShot(Crashed);
        GetComponent<RocketMovement>().enabled = false; // disables the movement script when the YouCrashed method plays 
        Invoke ("RestartFromCheckPoint" , LevelLoadDelayTime); //invoke allows the method to be delayed before it is activated
    }
    void LoadNextLevel() // new method created whereby the user is taken to the next level/scene when they have completed the course
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex; // current scene index is set to 0 so we will ad 1 to go to the next level
        int nextSceneIndex = currentSceneIndex + 1; // by adding this line of code, the game recognises what the next scene is
        if (nextSceneIndex == SceneManager.sceneCountInBuildSettings) // checks if the sceneindex variable is the same number as scenes in the game...
        {
            nextSceneIndex = 0; // loops back to the start scene (aka first level)       
        } 
        SceneManager.LoadScene(nextSceneIndex); // Scenemanager allows me to load a saved scene from unity
        // the scene being loaded is the one held in the variable
    }

    void RestartFromCheckPoint() // new method is declared
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex; // variable currentsceneindex will contain the active scene from scene manager
        SceneManager.LoadScene(currentSceneIndex); // the variable currentsceneindex will load the active scene +1 (aka the next level)
    }
}
   

Hi Rafi,

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.

Did this help you fix the problem?


See also:

hi Nina!

ive checked and the lines 43 and 36 is the problem, ive been on for hours trying to fix this, the only way i have bypassed this so that the other script (movement) works without delays and pressing “F” and the events/ next scenes actually play is when i remove audioSource completely from the script

anything with audioSource.playonshot is whats not working

Alright so, i managed to get rid of the null reference error - however the audio doesnt play at all (i managed to get rid of the error by adding the player tag to the rocket)

I don’t know how much you changed in your code but I’ve just spotted a little typo: The Start() method is spelt start(). It needs a capital S. Since you try to get the reference in the start method and since that method never got called, that is very likely the source of the issue.

If you found and fixed this typo yourself but the sound is still not working, please share your updated code here.

If you never hear any sound in Unity, check if the “Mute Audio” button in the game window is clicked.

i just realised my mistake thanks

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

Privacy & Terms