I’m pretty basic at C# and I’m trying to create player and it’s physics properties and I get an error when I try to debug the program and I need help. Here are my scripts:
Player:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float speed = 50f;
public float jumpPower = 150f;
public float maxSpeed = 3;
public bool grounded;
private Rigidbody2D rb2d;
private Animator anim;
void Start ()
{
rb2d = gameObject.GetComponent<Rigidbody2D>();
anim = gameObject.GetComponent<Animator>();
}
void Update ()
{
anim.SetBool("grounded", grounded);
anim.SetFloat("speed", Mathf.Abs(Input.GetAxis("Horizontal")));
}
void FixedUpdate()
{
//moving the player
float h = Input.GetAxis("Horizontal");
rb2d.AddForce((Vector2.right * speed) * h);
//limiting the speed of player
if (rb2d.velocity.x > maxSpeed)
{
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
}
if(rb2d.velocity.x < -maxSpeed)
{
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
}
}
}
GroundCheck:
using UnityEngine;
using System.Collections;
public class GroundCheck : MonoBehaviour {
private Player player;
void Start ()
{
player = gameObject.GetComponent<Player>();
}
void OnTriggerEnter2D (Collider2D col)
{
player.grounded = true;
}
void OnTriggerExit2D (Collider2D col)
{
player.grounded = false;
}
}
Here is the error: “NullReferenceException: object reference not set to an instance of an object. GroundCheck.OnTriggerEnter2D (UnityEngine.Collider2d col) (at Assets/Scripts/GroundCheck.cs:18)”
Please help me with this error ASAP.