NullReferenceException: Object reference not set to an instance of an object Groundcheck.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/object/code/Groundcheck.cs:17)
i keep getting a null reference exception error and i cant seem to solve it on my own.
here is my ground checking script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Groundcheck : MonoBehaviour {
private NewBehaviourScript player;
void Start()
{
player = gameObject.GetComponentInParent<NewBehaviourScript>();
}
void OnTriggerEnter2D(Collider2D col)
{
player.grounded = true;
}
void OnTriggerstay2D(Collider2D col)
{
player.grounded = true;
}
void OnTriggerExit2D(Collider2D col)
{
player.grounded = false;
}
}
and here is my character script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
public float maxspeed = 3;
public float speed = 50f;
public float jumpPower = 150f;
public bool grounded;
private Rigidbody2D rb2d;
private Animator anim;
void Start ()
{
rb2d = gameObject.GetComponent<Rigidbody2D>();
anim = gameObject.GetComponent<Animator>();
}
void Update ()
{
if (grounded && Input.GetButtonDown("Jump"))
{
rb2d.AddForce(Vector2.up * jumpPower);
}
}
private void NewMethod1()
{
transform.localScale = new Vector3(1, 1, 1);
}
private void NewMethod()
{
transform.localScale = new Vector3(-1, 1, 1);
}
private void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
rb2d.AddForce((Vector2.right * speed) * h);
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);
}
}
}
i'm new to this so any help i can get is very appreciated.
Your answer
Follow this Question
Related Questions
There's an issue with this script I can't find 0 Answers
Random Chance NullReferenceException: Object reference not set to an instance of an object 2 Answers
Help! Values from previous Serialized List<> shows up again after a while using Coroutine 0 Answers
Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers