Nullreferenceexception: Object Reference not set to an instance of an Object Motioncontroller.Fixedupdate

Asked

Viewed 152 times

1

I am following this Unity tutorial: http://unity3d.com/pt/learn/tutorials/modules/beginner/2d/2d-controllers

This was going great all the way to the part where you test to see if I’m touching the ground or not. After checking and trying to run the project this error appeared:

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 signaled in this function: grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);

If I delete the above line, the project already works, but does not do the intended

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

  • You call the method Start() before calling the FixedUpdate()?

  • This is the first time I’ve been playing Unity and I’m following the tutorial. In the video the Start() method was not called anywhere.

  • But the variable is null at this point, you must have forgotten something or the tutorial is wrong.

1 answer

1

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

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

Either you forgot to call this method or the variable must be instantiated elsewhere.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.