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