NullReferenceException warning

My code is getting this warning
NullReferenceException: Object reference not set to an instance of an object
Player_Movement.FixedUpdate () (at Assets/Scripts/Player_Movement.cs:118)

What is going wrong?

using UnityEngine;
using UnityEngine.InputSystem;
using System;
using System.Collections;

public class Player_Movement : MonoBehaviour
{
    // Using 2d as game is controled on X,Y

    //Public eaisy to edit
    public Rigidbody2D rb2d;

    public float yForce = 100f;

    public float xForce = 100f;

    public Transform groundCheck;

    public LayerMask groundLayer;
    
    bool jumpLockout;

    float direction = 0;

 //private to avoid changing
    private bool isGrounded;

    private bool canDash = true;
    private bool isDashing;
    public float DashForce = 10f;
    public float DashTime = .3f;
    public float dashCooldown = 1f;

//Cheacks for controls and enables controler support all new controls aspects need to be added below here but above the Void Awake function.
    InputAction jumpAction;
    InputAction dashAction;

    PlayerControls controls;

    private void Awake()
    {

        if(isDashing)
        {
            return;
        }
        controls = new PlayerControls();
        controls.Enable();
//This was only needed for -/+ axis thank god
        /*controls.Movement.Move.performed += ctx => 
        {
            direction = ctx.ReadValue<float>();
        };*/
//Formating for all future Imput actions below "_Action = InputSystem.actions.FindAction("_");

        jumpAction = InputSystem.actions.FindAction("Jump");
        // Jumping Input action

        dashAction = InputSystem.actions.FindAction("Dash");
    }

private void OnCollisionEnter2D(Collision2D collision)
{
    //Ground collision to check if jumping is possible, make sure to apply Tag=Ground to anything I want the player to jump off of.
    if (collision.gameObject.tag == "Ground")
    {
        isGrounded = true;
    } 
}

    void FixedUpdate ()
    {

        if(isDashing)
        {
            return;
        }
    //Basic movement
        //Need to figure out a way to make this reset Velocity to zero when nuter at a contolled pase or 
        // switch to AddForce2D which can lead to whole host of problems later. (problem below)
        /*controls.Movement.Move.started += ctx => 
        {
            direction = ctx.ReadValue<float>();
        };*/
        /*if(dashTime > 0){
            dashTime--;
            rb2d.gravityScale = 0;
            rb2d.linearVelocity = new Vector2(rb2d.linearVelocity.x, 0);
        } else{
            rb2d.gravityScale = 1;
        }*/
        direction = controls.Movement.Move.ReadValue<float>();
        if(Math.Round(direction, 2) != 0){
            rb2d.linearVelocity = new Vector2(direction * xForce, rb2d.linearVelocity.y);
        } else{
            rb2d.linearVelocity = new Vector2(rb2d.linearVelocity.x * 0.95f, rb2d.linearVelocity.y);
        }

            if (jumpAction.IsPressed() && isGrounded)
            {
                isGrounded = false;
                rb2d.AddForce(Vector2.up * yForce);
                //jumpLockout = true;
                /*if(isGrounded)
                    {
                        rb2d.AddForce(Vector2.up * yForce);
                    }*/
            } else{
                //jumpLockout = false;
            }
            /*jumpAction.started += context =>{
            if(jumpAction.IsPressed() && !isGrounded && (dashTime == 0)){
                dashTime = maxDashTime;

            }
            };*/
        //Jumping
        if (dashAction.IsPressed() && canDash)
        {
            StartCoroutine(Dash());
        }
        
}

    private IEnumerator Dash()
    {
        canDash = false;
        isDashing = true;
        float originalGravity = rb2d.gravityScale;
        rb2d.gravityScale = 0f;
        rb2d.linearVelocity = new Vector2(direction * DashForce, 0f);
        yield return new WaitForSeconds(DashTime);
        rb2d.gravityScale = originalGravity;
        isDashing = false;
        yield return new WaitForSeconds(dashCooldown);
        canDash = true;
    }
}

the code runs?

your dashAction is not set for a certain moment in time. it starts as null. then you set it in the awake but before that is null. that is why you get the warning

I deleted the part in awake and it has the same warning

Forgot to save the dash input asset sorry

1 Like

Awake() happens before anything else in the script happens. That’s a pretty basic thing that you should be aware of.

I mean you can see in the code they posted that the code that would assign the input action is commented out, so all the information was in the code.

I just wanted to point out the incorrect information, so that anyone else reading this thread in future isn’t sent on a wild goose chase.

Edit for future readers: The post above this one was deleted.

1 Like

yeah, replying to a comment I’ve made that you manage to hide. how I can never manage to report and hide a comment when there is someone posting links to malicious websites? stop using your alternative moderator account to censorship users