Hey guys, this is probably a very n00b question, but I just started using unity and I’m at the part of the space shooter tutorial where I’m implementing a script to make the player move, and constrain it within the game map… yet I’m getting the error: “Object reference not set to an instance of an object.”
As far as I can tell, I’ve written my code exactly like the tutorial, so I have no idea what it could be… here it is though, since it’s not very long; I was hoping maybe someone can read through and see if any dumb mistake really sticks out at them, because I would appreciate that SO much. Thank you!
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public Boundary boundary;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
}
}