so I am making my very first game in unity and I’m having problems with the jump part of the script.
every time I press spacebar I get this error:
"
NullReferenceException: Object reference not set to an instance of an object
PlayerMovement.IsGrounded () (at Assets/Scripts/PlayerMovement.cs:40)
PlayerMovement.Update () (at Assets/Scripts/PlayerMovement.cs:19)
"
script:
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float movementSpeed;
public Rigidbody2D rb;
public float JumpForce;
public Transform feet;
public LayerMask groundLayers;
float mx;
public void Update()
{
mx = Input.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump") && IsGrounded()) {
Jump();
}
}
public void FixedUpdate() {
Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);
rb.velocity = movement;
}
void Jump() {
Vector2 movement = new Vector2(rb.velocity.x, JumpForce);
rb.velocity = movement;
}
public bool IsGrounded() {
Collider2D groundCheck = Physics2D.OverlapCircle(feet.position, 0.5f, groundLayers);
if (groundCheck.gameObject != null) {
return true;
}
return false;
}
}