I was following this tutorial: http://unity3d.com/pt/learn/tutorials/modules/beginner/2d/2d-controllers
It was going well until the ground check part.
I dont know why but everytime I try to run the project this error occurs:
> NullReferenceException: Object reference not set to an instance of an object>MotionController.FixedUpdate () (at Assets/Scripts/MotionController.cs:40)
Here is my C# code:
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();
//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().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().velocity = new Vector2(move * maxSpeed, GetComponent().velocity.y);
//END TUTORIAL
bool jetpackActive = Input.GetButton("Fire1");
if (jetpackActive)
{
// GetComponent().AddForce(new Vector2(0, jetpackForce));
}
// Vector2 newVelocity = GetComponent().velocity;
// newVelocity.x = forwardMovementSpeed;
// GetComponent().velocity = newVelocity;
}
}
Thanks for your time
↧