1

First of all, how original of me to post another dreaded

NullReferenceException: Object reference not set to an instance of an object

but I have scoured the web looking for a solution for like 2 hours now and have come up with nothing... Here is are the two scripts i have :

GROUNDED:

using UnityEngine;
using System.Collections;

public class GroundCheck : MonoBehaviour {

private Player player;

void Start()
{
    player = GetComponent<Player>();
}

void OnTriggerEnter2D(Collider2D col)
{
    player.grounded = true;
}

void OnTriggerExit2D(Collider2D col)
{
    player.grounded = false;
}
}

PLAYER:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

public float maxSpeed = 3;
public float speed = 50f;
public float jumpPower = 150f;

public bool grounded;

private Rigidbody2D rb2d;
private Animator anim;

// Use this for initialization
void Start () {

    rb2d = gameObject.GetComponent<Rigidbody2D>();
    anim = gameObject.GetComponent<Animator>();
}

// Update is called once per frame
void Update () {

    anim.SetBool("Grounded", grounded);
    anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
}

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

The exact error is:

NullReferenceException: Object reference not set to an instance of an object

GroundCheck.OnTriggerEnter2D (UnityEngine.Collider2D col) (atAssets/scripts/GroundCheck.cs:15)

Here is my scene:

Here is my boxcollider (if it helps):

boxcollider

Grant Winney
  • 61,140
  • 9
  • 100
  • 152
rob rob
  • 13
  • 3
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Mohit Shrivastava Jan 13 '16 at 03:20

3 Answers3

1

Your ground check script isn't on the same object as the player script, that means you can't use getcomponent to get the player script. So you haven't set the player var to anything which is causing the error. Set the player var to the gameobject that has the player script in the editor then in your start method use player.GetComponent();

PlanetVaster
  • 499
  • 1
  • 6
  • 18
1

If both of the GroundCheck and PLAYER classes are on same GameObject then change the Start() method of GroundCheck class like this:

void Start()
{
    player = gameObject.GetComponent<Player>();
}

If they are not on same GameObject then use the following code:

void Start()
{
    GameObject playerObj = GameObject.Find("Name of gameObject that player script is in that");
    player = playerObj.GetComponent<Player>();
}

In PLAYER class add static modifier to defination of grounded:

public static bool grounded;
Hossein Rashno
  • 2,362
  • 1
  • 20
  • 44
-1

void OnTriggerEnter2D(Collider2D col) <-- in collider param request gameObject, getcomponent to col is prefered, only control if object collision is player. col.gameObject.getcomponent<Player>().grounded=true;

if(col.Name.Equals("Player")
{
    col.gameObject.getcomponent<Player>().grounded=true;
}

I had a similar problem. I hope it's helps http://docs.unity3d.com/ScriptReference/Collision2D.html

Collider2d have gameobject component, trigger enter get Collider this object.

in http://docs.unity3d.com/ScriptReference/Collider2D.OnCollisionEnter2D.html see example use in collider (not trigger is only example) to use, acces gameObject.

Not necessary findtag when object(player) is passing for parameter in event OnTriggerEnter, Exit or Stay