Space Shooter Tutorial Script Not Working

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)
		);
	}
}

You need to post the line number the error refers to (or, just cut/paste the actual error). And, make sure the line number it refers to coincides with the lines as listed in the posted script.

That said, maybe the gameObject your script is associated with doesn’t have a rigidbody component?

Jeff

Woah, that’s odd, I was just getting the errors it gave me, when I ran the game again and it worked… I wish I knew what I did differently, but thanks anyways! At least I’m back on the right track now! :slight_smile: