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