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

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #11968
    Auten Diver
    Participant

    “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:

    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--;
        	}
        }
    }

    Thanks!

    #11970
    Terence
    Keymaster

    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 put print(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:

    #11971
    Auten Diver
    Participant

    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.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: