Unity 2D: Game freezes after the first frame of a particular animation
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 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");
         
     }
 }
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.
Answer by Larry-Dietz · Dec 05, 2019 at 06:29 PM
The error is telling me that either skok is null or skok.rb is null.
Are both jump and dust attached to the same gameobject? Or is dust attached to a child object of the object with the jump script?
If that is the case, try the following change...
 Change   
 
  skok = GetComponent<jump>();
 
 to
 
  skok = transform.parent.gameObject.GetComponent<jump>();
Hope this helps, -Larry
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                