Game freezes after particular animation frame

Hello, I am fairly new to Unity and coding so excuse me if I sound out of touch. I am making this 2D platformer/sidescroller and I have this sprite for dust trails for when he runs/jumps and since I don’t know how to implement them via the particle system I figured I would create a new game object, attach the dust animations to it, attach it to my character as a child and trigger it via script whenever I run/jump. However, when I set up all the animations and trigger them the game freezes on the very first frame of said animation and I get the following error in the console.

NullReferenceException: Object reference not set to an instance of an object dust.Update () (at Assets/dust.cs:18)

I created an entirely new project with minimal code just to see if it was one of the other scripts affecting it but sadly no. Here’s the new test code that I used for the character (just a jump script basically)

using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
  
    public class jump : MonoBehaviour {
  
        public Rigidbody2D rb;
  
        void Start () {
            rb = GetComponent<Rigidbody2D>();
          
        }
      
        void Update () {
            if (Input.GetKeyDown(KeyCode.Space))
                rb.AddForce(new Vector2(0, 300));
        }
  
        void FixedUpdate()
        {
            rb.velocity = new Vector2(0, rb.velocity.y);
          
        }
    }

And here’s the script for the game object I attached the dust animations to -

using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
  
    public class jump : MonoBehaviour {
  
        public Rigidbody2D rb;
  
        void Start () {
            rb = GetComponent<Rigidbody2D>();
          
        }
      
        void Update () {
            if (Input.GetKeyDown(KeyCode.Space))
                rb.AddForce(new Vector2(0, 300));
        }
  
        void FixedUpdate()
        {
            rb.velocity = new Vector2(0, rb.velocity.y);
          
        }
    }

The weird part is the game only freezes if the animation is in any way triggered. I can let the animation loop as I’m playing and there’s no problem whatsoever, but once I trigger it via code the I get this error and the game just freezes.

Honestly I’m sorry if it’s a dumb question, as I said I’m only a week or so into Unity and C#, thanks in advance.

The weird part is you didn’t show us the “dust” script that’s causing the issue on line 18. The object is null, which means all you probably need to do is drag and drop whatever game object it is in that field. But we cannot see the variable to know. Maybe you’re having an issue Finding the target object? It’s really hard to say, but this is definitely your issue and can be easily resolved.

Sorry I seem to have gotten the scripts mixed up somehow. But yeah what you suggested did work so thank you !
Here’s the script in case you were curious.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dust : MonoBehaviour {
    Animator anim;
    jump skok;
    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator>();
        skok = GetComponent<jump>();

    }

    // Update is called once per frame
    void Update () {
        if (skok.rb.velocity.y > 0)
            anim.SetTrigger("go");

    }
}
1 Like

I wasn’t “curious” only intrigued, my friend :wink:

lol

I’m glad you fixed it, amigo :slight_smile: