Rocket keeps crashing when next level is loaded

My rocket keeps crashing at the launching pad when the next level is loaded. Console tells me this:
NullReferenceException: Object reference not set to an instance of an object
CollisionHandler.StartCrashSequence () (at Assets/Script/CollisionHandler.cs:58)

By the way, can you teach me how to properly copy and paste my codes here? My posting seems not working like other people’s:
using System;

using UnityEngine;

using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour

{

[SerializeField] float levelLoadDelay = 2f;

[SerializeField] AudioClip success;

[SerializeField] AudioClip crash;

[SerializeField] ParticleSystem successParticles;

[SerializeField] ParticleSystem crashParticles;

AudioSource audioSource;

//This is the 1st time I see a State used.

bool isTransitioning = false;

void Start()

{

    audioSource = GetComponent<AudioSource>();

}

private void OnCollisionEnter(Collision other)

{

    if(isTransitioning){return;}

    switch (other.gameObject.tag)

    {

        case "Friendly":

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

            break;

        case "Finish":

            StartSuccessSequence();

            break;

        case "Fuel":

            Debug.Log("You picked up fuel");

            break;

        default:

            StartCrashSequence();

            break;

    }

}

void StartSuccessSequence()

{

    isTransitioning = true;

    audioSource.PlayOneShot(success);

    successParticles.Play();

    GetComponent<Movement>().enabled = false;

    Invoke("LoadNextLevel", levelLoadDelay);

}

void StartCrashSequence()

{

    isTransitioning = true;

    audioSource.PlayOneShot(crash);

    crashParticles.Play();

    // to add particle effect upon crash

    GetComponent<Movement>().enabled = false;

    Invoke ("ReloadLevel", levelLoadDelay);

}

void LoadNextLevel()

{

    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

    int nextSceneIndex = currentSceneIndex + 1;

    if(nextSceneIndex == SceneManager.sceneCountInBuildSettings)

    {

        nextSceneIndex = 0;

    }

    SceneManager.LoadScene(nextSceneIndex);

}

void ReloadLevel()

{

    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

    SceneManager.LoadScene(currentSceneIndex);

}

}

You can put your entire code between 3 grave symbols top and 3 bottom;

Schermafbeelding 2022-04-17 114602

and then it shows like this;

code here
1 Like

On first read through I see two possible errors

  • the audioClip crash could not be set in the unity editor
  • the crashParticle could not be set in the unity editor

null-reference means that one of the object you are using is not set and cannot be found.
since the error message says “StartCrashSequence” it probably is a problem in that function

Thanks for the help. I found I might not have described my problem correctly. It’s not my rocket keeps crashing, but it keeps giving out audio and particles of crashing. After comparing my codes with Rick’s, I found I had missed

audioSource.Stop();

I now have added it. I don’t hear the audio now, but the particles are still not stopped.
I feel strange that the codes I now have are exactly the same as Rick’s, but I still have this problem. I don’t know what is wrong.
Let me post the complete codes of CollisionHandlers.cs below:

using System;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour
{
    [SerializeField] float levelLoadDelay = 2f;
    [SerializeField] AudioClip success;
    [SerializeField] AudioClip crash;
    [SerializeField] ParticleSystem successParticles;
    [SerializeField] ParticleSystem crashParticles;

    AudioSource audioSource; 

    //This is the 1st time I see a State used.
    bool isTransitioning = false;

    void Start()
    {
        audioSource = GetComponent<AudioSource>();
    }

    private void OnCollisionEnter(Collision other) 
    {
        if (isTransitioning) { return; }

        switch (other.gameObject.tag)
        {
            case "Friendly":
                Debug.Log("This thing is friendly");
                break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartCrashSequence();
                break;
        }
    }

    void StartSuccessSequence()
    {
        isTransitioning = true;
        audioSource.Stop();
        audioSource.PlayOneShot(success);
        successParticles.Play();

        GetComponent<Movement>().enabled = false;
        Invoke("LoadNextLevel", levelLoadDelay);
    }

    void StartCrashSequence()
    {
        isTransitioning = true;
        audioSource.Stop();
        audioSource.PlayOneShot(crash);
        crashParticles.Play();
        GetComponent<Movement>().enabled = false;
        Invoke ("ReloadLevel", levelLoadDelay);
    }
    void LoadNextLevel()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        int nextSceneIndex = currentSceneIndex + 1;
        if(nextSceneIndex == SceneManager.sceneCountInBuildSettings)
        {
            nextSceneIndex = 0;
        }
        SceneManager.LoadScene(nextSceneIndex);
    }

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

So what is at your “collisionhandler” line 58, if i copy your script for me it says;

GetComponent<Movement>().enabled = false;

If thats the case, it looks like it cant find the Movement component.

The thing about your rocket crashing, means it reached default here ;

    private void OnCollisionEnter(Collision other) 
    {
        if (isTransitioning) { return; }

        switch (other.gameObject.tag)
        {
            case "Friendly":
                Debug.Log("This thing is friendly");
                break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartCrashSequence();
                break;
        }
    }

The first thing i would think then, is that you maybe forgot to set the platform tag?
Or maybe the collider is sticking out and touching something else?

I see i read wrong in your last update, and your sound is fixed,
you dont happen to have clicked Looping in the particles?

Sounds like it.

It is now line 57 after I updated it.

crashParticles.Play();

The complete console notice is:

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

You’re right. Line 36 is mentioned. So yes it means it reached default here.
When you say the platform tag, do you mean the launch pad? I have set its tag.

No. I haven’t clicked Looping in the particles.

Yes i did mean the launch pad tag, if thats not set right, the collision will not be handled right.
The starting one should be friendly, so nothing happens, well only a debug.log…
The end should be finish so the successequence gets triggered.
The rest of your level should be default so it will trigger the CrashSequence.

It shows a nullreference on line 57, which you said is the “crashParticles.Play”
That means it cannot find any particles, did you drag it into the serialize field in the editor?

Thanks for your further help.
Yes. I have dragged it into the serialized field.
The problem is it keeps releasing particles. I have more than enough. Not null at all. How strange.
I’ve recorded a clip of the Play video, but it seems I can’t upload here.

for videos under 1 min, you could use https://imgur.com/,
for videos under 250mb you could use https://streamable.com.
If its too long or large, i usually just use Youtube and make it viewable by link only.

1 Like

Thanks. Here it is:

Can you send some screenshots from your Inspector;
with the rocket selected, with the succes particles and one with the explosion particles.

In the script it all looks to be in order, must be something in the editor.



these screenshots are from, your first level or the second?

They are from the first level.
Below are from the second level:



Hmm i dont see anything off with this, at this point i personally have no clue what it could be.
Maybe @Nina has an idea?

1 Like

Have you already tried to add Debug.Logs to your code to see what is going on during runtime, @lp300? Since the issue occurs when the next level gets loaded, it would be interesting to know if the rocket collides with another collider and if that other colliders causes the crashing.

The crashing gets triggered in your OnCollisionEnter method, and you are checking tags there. Perhaps the rocket collides with something that does not have the correct tag assigned, e.g. the “friendly” platform does not have the “Friendly” tag.

1 Like

Thanks! I find the problem! After reading your post, I realized the key could be the position of the Rocket in the second level. So I just lifted the rocket in the second scene a little bit upward. It should be that the Rocket was contacting with the Ground. I had added an object to the bottom of the Rocket in first level and applied overriding the Prefab without noticing it could have affected the relative position of the Rocket to the Ground.

Privacy & Terms