Forum begins after the advertisement:
[Part 2] Null Reference for Update and UpdateJump Variables
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 2] Null Reference for Update and UpdateJump Variables
- This topic has 2 replies, 2 voices, and was last updated 1 year, 2 months ago by Auten Diver.
-
AuthorPosts
-
October 2, 2023 at 5:20 pm #11968::
“NullReferenceException: Object reference not set to an instance of an object PlayerController.UpdateJumpVariables () (at Assets/Scripts/PlayerController.cs:139) PlayerController.Update () (at Assets/Scripts/PlayerController.cs:58)”
I have never coded or used Unity before, things are going relatively smoothly, i just don’t know how to properly identify a lot of problems and fix them. Here’s where I am with the code so far:
<code>using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { [Header("Horizontal Movement Settings")] [SerializeField] private float walkSpeed = 1; [Header("Ground Check Settings")] [SerializeField] private float jumpForce = 45; private int jumpBufferCounter = 0; [SerializeField] private int jumpBufferFrames; [SerializeField] private Transform groundCheckPoint; [SerializeField] private float groundCheckY = 0.2f; [SerializeField] private float groundCheckX = 0.5f; [SerializeField] private LayerMask whatIsGround; PlayerStateList pState; private Rigidbody2D rb; private float xAxis; Animator anim; bool attack = false; float timeBetweenAttack, timeSinceAttack; public static PlayerController Instance; private void Awake() { if(Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } } // Start is called before the first frame update void Start() { pState = GetComponent<PlayerStateList>(); rb = GetComponent<Rigidbody2D>(); anim = GetComponent<Animator>(); } // Update is called once per frame void Update() { GetInputs(); UpdateJumpVariables(); Flip(); Move(); Jump(); //Attack(); } void GetInputs() { xAxis = Input.GetAxisRaw("Horizontal"); attack = Input.GetMouseButtonDown(0); } void Flip() { if(xAxis < 0) { transform.localScale = new Vector2(-1, transform.localScale.y); } else if(xAxis > 0) { transform.localScale = new Vector2(1, transform.localScale.y); } } private void Move() { rb.velocity = new Vector2(walkSpeed * xAxis, rb.velocity.y); anim.SetBool("Walking", rb.velocity.x != 0 && Grounded()); } //void Attack() //{ //timeSinceAttack =+ time.deltaTime; //if(attack && timeSinceAttack >= timeBetweenAttack) //{ //timeSinceAttack = 0; //anim.setTrigger("Attacking"); //} //} public bool Grounded() { if(Physics2D.Raycast(groundCheckPoint.position, Vector2.down, groundCheckY, whatIsGround) || Physics2D.Raycast(groundCheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround) || Physics2D.Raycast(groundCheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround)) { return true; } else { return false; } } void Jump() { if(Input.GetButtonUp("Jump") && rb.velocity.y > 0) { rb.velocity = new Vector2(rb.velocity.x, 0); pState.jumping = false; } if(!pState.jumping) { if (jumpBufferCounter > 0 && Grounded()) { rb.velocity = new Vector3(rb.velocity.x, jumpForce); pState.jumping = true; } } anim.SetBool("Jumping", !Grounded()); } void UpdateJumpVariables() { if (Grounded()) { pState.jumping = false; } if (Input.GetButtonDown("Jump")) { jumpBufferCounter = jumpBufferFrames; } else { jumpBufferCounter--; } } }</code>
Thanks!
October 2, 2023 at 9:12 pm #11970::Hi Auten, sorry that the forum flagged your post as spam.
Your issue should be this line:
pState.jumping = false;
Specifically,
pState
is null, and if you putprint(pState);
before that line it will confirm this. You will need to find out why it is null.Here is a video which might help:
October 3, 2023 at 10:50 am #11971::No problem, better an overzealous spam filter than a lackadaisical one.
I determined why I was getting the Null Reference, thanks for sharing the video, turns out I didn’t need to pore over the code multiple times. Unfortunately, now I am still unable to jump, at least no errors.
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: