Object referencing problem (?)

So I was following along by this tutorial, on how to make a drivable car in Unity. After I got to the point when you press the play button and hope for the best, I got an error, which was saying:

NullReferenceException: Object reference not set to an instance of an object
PlayerCar_Script.Update () (at Assets/PlayerCar_Script.js:65)

Since I’m new to the Unity and C#/JS scripting, there is probably some stupid mistake I am making. Here are the codes:

PlayerCar_Script.js

// These variables allow the script to power the wheels of the car.
var frontLeft : WheelCollider;
var frontRight : WheelCollider;


// These variables are for the gears, the array is the list of ratios. The script
// uses the defined gear ratios to determine how much torque to apply to the wheels.
private var GearRatio : float[];
var CurrentGear : int = 0;


// These variables are just for applying torque to the wheels and shifting gears.
// using the defined Max and Min Engine RPM, the script can determine what gear the
// car needs to be in.
var EngineTorque : float = 600.0;
var MaxEngineRPM : float = 3000.0;
var MinEngineRPM : float = 1000.0;
private var EngineRPM : float = 0.0;


function Start () {
	// I usually alter the center of mass to make the car more stable. I'ts less likely to flip this way.
	rigidbody.centerOfMass.y = -1.5;
}

function ShiftGears() {
	// this funciton shifts the gears of the vehcile, it loops through all the gears, checking which will make
	// the engine RPM fall within the desired range. The gear is then set to this "appropriate" value.
	if ( EngineRPM >= MaxEngineRPM ) {
		var AppropriateGear : int = CurrentGear;
		
		for ( var i = 0; i < GearRatio.length; i ++ ) {
			if ( frontLeft.rpm * GearRatio[i] < MaxEngineRPM ) {
				AppropriateGear = i;
				break;
			}
		}
		
		CurrentGear = AppropriateGear;
	}
	
	if ( EngineRPM <= MinEngineRPM ) {
		AppropriateGear = CurrentGear;
		
		for ( var j = GearRatio.length-1; j >= 0; j -- ) {
			if ( frontLeft.rpm * GearRatio[j] > MinEngineRPM ) {
				AppropriateGear = j;
				break;
			}
		}
		
		CurrentGear = AppropriateGear;
	}
}


function Update () {

	// This is to limith the maximum speed of the car, adjusting the drag probably isn't the best way of doing it,
	// but it's easy, and it doesn't interfere with the physics processing.
	rigidbody.drag = rigidbody.velocity.magnitude / 250;
	
	// Compute the engine RPM based on the average RPM of the two wheels, then call the shift gear function
	EngineRPM = (frontLeft.rpm + frontRight.rpm)/2 * GearRatio[CurrentGear];
	ShiftGears();

	// finally, apply the values to the wheels.	The torque applied is divided by the current gear, and
	// multiplied by the user input variable.
	frontLeft.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
	frontRight.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
		
	// the steer angle is an arbitrary value multiplied by the user input.
	frontLeft.steerAngle = 10 * Input.GetAxis("Horizontal");
	frontRight.steerAngle = 10 * Input.GetAxis("Horizontal");
}

WheelAlignment_Script.js

// Define the variables used in the script, the Corresponding collider is the wheel collider at the position of
// the visible wheel, the slip prefab is the prefab instantiated when the wheels slide, the rotation value is the
// value used to rotate the wheel around it's axel.
var CorrespondingCollider : WheelCollider;
var SlipPrefab : GameObject;
private var RotationValue : float = 0.0;


function Update () {

	// define a hit point for the raycast collision
	var hit : RaycastHit;
	// Find the collider's center point, you need to do this because the center of the collider might not actually be
	// the real position if the transform's off.
	var ColliderCenterPoint : Vector3 = CorrespondingCollider.transform.TransformPoint( CorrespondingCollider.center );
	
	// now cast a ray out from the wheel collider's center the distance of the suspension, if it hit something, then use the "hit"
	// variable's data to find where the wheel hit, if it didn't, then se tthe wheel to be fully extended along the suspension.
	if ( Physics.Raycast( ColliderCenterPoint, -CorrespondingCollider.transform.up, hit, CorrespondingCollider.suspensionDistance + CorrespondingCollider.radius ) ) {
		transform.position = hit.point + (CorrespondingCollider.transform.up * CorrespondingCollider.radius);
	}else{
		transform.position = ColliderCenterPoint - (CorrespondingCollider.transform.up * CorrespondingCollider.suspensionDistance);
	}
	
	// now set the wheel rotation to the rotation of the collider combined with a new rotation value. This new value
	// is the rotation around the axle, and the rotation from steering input.
	transform.rotation = CorrespondingCollider.transform.rotation * Quaternion.Euler( RotationValue, CorrespondingCollider.steerAngle, 0 );
	// increase the rotation value by the rotation speed (in degrees per second)
	RotationValue += CorrespondingCollider.rpm * ( 360/60 ) * Time.deltaTime;
	
	// define a wheelhit object, this stores all of the data from the wheel collider and will allow us to determine
	// the slip of the tire.
	var CorrespondingGroundHit : WheelHit;
	CorrespondingCollider.GetGroundHit( CorrespondingGroundHit );
	
	// if the slip of the tire is greater than 2.0, and the slip prefab exists, create an instance of it on the ground at
	// a zero rotation.
	if ( Mathf.Abs( CorrespondingGroundHit.sidewaysSlip ) > 2.0 ) {
		if ( SlipPrefab ) {
			Instantiate( SlipPrefab, CorrespondingGroundHit.point, Quaternion.identity );
		}
	}

}

I double checked the referencing of the wheel colliders and the car wheels and did the same with PlayerCar_Script.js and the Car references at my project in Unity, but I still got the error message. Hope you can help me with this. Thanks!

Oh, and I changed var GearRatio : float[]; to private var GearRatio : float[]; at the 8th line in the PlayerCar_Script.js, because I was getting “IndexOutOfRangeException: Array index is out of range.” error message.

This error always means you didn’t assign something (usually in the inspector) before trying to use it. The object is still null or unassigned, so when you call myObject.something it can’t find the target because it doesn’t exist. Can you make sure your line numbers are correct in the examples you posted? Because line 65 doesn’t look like it would throw this error. (Just look at the contents of line 65 and see if there’s an object being referenced that might not be assigned yet.)

I would make sure everything has been assigned in the inspector properly like frontLeft and frontRight. If you leave one of these blank, you’ll definitely get that error.

Changing that array to private won’t help you with that error. IndexOutOfRangeException means you are trying to assign or read an element in an array which does not exist. Either your array hasn’t been initialized first (new type) or you’re trying to access an element it does not have. For example, trying to access element 15 in a 10 element array. var myArray = new float[10]; myArray[15] = 4.0f; This will give you that error.

Thanks for the reply.
About that 65th line. I get a bunch of error messages saying “NullReferenceException: Object reference not set to an instance of an object
PlayerCar_Script.Update () (at Assets/PlayerCar_Script.js:65)” when I try to run the game. I don’t know why it addresses to that line. And one more thing, the 65th line in my code is

EngineRPM = (frontLeft.rpm + frontRight.rpm)/2 * GearRatio[CurrentGear];

This is probably because I didn’t copied some blank space. Anyways, I don’t know what I am doing wrong here. I got my wheel colliders attached to every single wheel of the car.
There might be a problem in the code, because I copied it from the tutorial and changed the names of wheels and such. But it seems to me, that I did that job pretty well. Maybe some of that code doesn’t make sense with my project? As I said before, I’m new to the JS programming, so maybe I put a little bit too much of the tutorial’s code into my small project?
Here’s the project

The problem is that GearRatio is never set to anything and is therefore null. By making it a private array, it must be initialized somewhere in your code before you can use it, which you have not done anywhere. I have a feeling that variable was supposed to have some data in it assigned in the inspector. Now that its private, if you set any values in that array in the inspector, they have been lost.

You should change GearRatio back to a public var and check the tutorial and see if you were supposed to assign any values to this array in the inspector.

Edit: It’s right in the tutorial. You must specify these gear ratios in that array:

GEAR RATIOS
◦1st Gear : 4.31 : 1.0
◦2nd Gear : 2.71 : 1.0
◦3rd Gear : 1.88 : 1.0
◦4th Gear : 1.41 : 1.0
◦5th Gear : 1.13 : 1.0
◦6th Gear : 0.93 : 1.0

Set this in the inspector for the GearRatio field. (First set the size of the field to 6, then it will give you 6 spaces in which to fill in these values). Note: Just put the 4.31 in the field, not the : 1.0 part.

You are my hero! Thanks, that was the problem.