NullReferenceException: Object reference not set to an instance of an object MotionController.FixedUpdate

1

I'm following this unity tutorial: link

This was going well to the test site to see if I'm touching the ground or not. After doing the verification and trying to run the project appeared this error:

  

NullReferenceException: Object reference not set to an instance of an object   MotionController.FixedUpdate () (at Assets / Scripts / MotionController.cs: 40)

The code is very similar to the tutorial:

using UnityEngine;
using System.Collections;

public class MotionController : MonoBehaviour {

    public float maxSpeed = 10f;                                                //Velocidade maxima do boneco

    //    public float jetpackForce = 75.0f;
    //    public float forwardMovementSpeed = 0.5f;

    private Animator anim;

    private bool grounded;
    public Transform groundCheck;
    float groundRadius = 0.2f;
    public LayerMask whatIsGround;
    public float jumpForce = 700f;



    // Use this for initialization
    void Start () {
        //START TUTORIAL
        anim = GetComponent<Animator>();
        //END TUROTIAL
        groundCheck = transform.Find("groundCheck");
    }

    // Update is called once per frame
//  void Update () {
//        if(grounded && Input.GetKeyDown(KeyCode.Space))
//        {
//            anim.SetBool("Ground", false);
//            GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
//        }
//    }

    void FixedUpdate()
    {
        grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
        anim.SetBool("Ground", grounded);

        //START TUTORIAL -- ANDAR
        float move = Input.GetAxis("Horizontal");

        anim.SetFloat("Speed", Mathf.Abs(move));

        GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
        //END TUTORIAL


        bool jetpackActive = Input.GetButton("Fire1");

        if (jetpackActive)
        {
//            GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jetpackForce));
        }
//        Vector2 newVelocity = GetComponent<Rigidbody2D>().velocity;
//        newVelocity.x = forwardMovementSpeed;
//        GetComponent<Rigidbody2D>().velocity = newVelocity;
    }
 }

The error is flagged in this function: grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);

If I delete the above line, the project already works but does not do what it wants.

Now the project does nothing because of the error listed above. Thanks for the help

    
asked by anonymous 14.12.2015 / 17:22

1 answer

1

The problem is that the variable groundCheck is not being initialized, so NullReferenceException

This variable is being initialized into the Start() method, but as you said in the comments, you are not calling the Start() method.

Or you forgot to call this method or the variable should be instantiated elsewhere.

    
14.12.2015 / 17:36