- Home /
Null reference exception in script
hi, I'm trying to write a code for a car controller but I'm getting this error… please check my script and let me know what the problem is…. I've looked up unity answers and found lots of questions on this topic, but i could not fix my problem using them.
heres my script
//variables
private var accel : Vector3;
public var throttle : float;
private var deadZone = 0.001 ;
private var myRight : Vector3;
private var velo : Vector3;
private var flatVelo : Vector3;
private var relativeVelocity : Vector3;
private var dir : Vector3;
private var flatDir : Vector3;
private var carUp : Vector3;
private var carTransform : Transform;
private var carRigidbody : Rigidbody;
private var engineForce : Vector3;
private var turnVec : Vector3;
private var imp : Vector3;
private var rev : float;
private var actualTurn : float;
private var carMass : float;
private var wheelTransform : Transform[] = new Transform[4];
public var actualGrip : float;
public var horizontal : float;
private var maxSpeedToTurn : float = .2;
//cars wheels
public var frontLeftWheel : Transform;
public var frontRightWheel : Transform;
public var rearLeftWheel : Transform;
public var rearRightWheel : Transform;
//transforms for steering
public var LFWheelTransform : Transform;
public var RFWheelTransform : Transform;
//car physics
public var power : float = 300;
public var maxSpeed : float = 50;
public var carGrip : float = 70;
public var turnSpeed : float = 3.0;
private var slideSpeed : float;
public var mySpeed : float;
private var carRight : Vector3;
private var carFwd : Vector3;
private var tempVEC : Vector3;
function Initialize()
{
carTransform = transform;
carRigidbody = rigidbody;
carUp = carTransform.up;
carMass = rigidbody.mass;
carFwd = Vector3.forward;
carRight = Vector3.right;
setUpWheels();
carRigidbody.centerOfMass = Vector3(0,-0.7,0.35);
}
function Update()
{
carPhysicsUpdate();
checkInput();
}
function LateUpdate()
{
rotateVisualWheels();
engineSound();
}
function setUpWheels()
{
if((null == frontLeftWheel || null == frontRightWheel || null == rearLeftWheel || null == rearRightWheel))
{
Debug.Log("wheel transforms not plugged in");
//Debug.break;
}
else
{
wheelTransform[0] = frontLeftWheel;
wheelTransform[1] = frontRightWheel;
wheelTransform[2] = rearLeftWheel;
wheelTransform[3] = rearRightWheel;
}
}
private var rotationAmount : Vector3;
function rotateVisualWheels()
{
LFWheelTransform.localEulerAngles.y = horizontal * 30;
RFWheelTransform.localEulerAngles.y = horizontal * 30;
rotationAmount = carRight * (relativeVelocity.z*1.6*Time.deltaTime*Mathf.Rad2Deg);
wheelTransform[0].Rotate(rotationAmount);
wheelTransform[1].Rotate(rotationAmount);
wheelTransform[2].Rotate(rotationAmount);
wheelTransform[3].Rotate(rotationAmount);
}
private var deviceAccelerometerSensitivity : float = 2;
function checkInput()
{
if ((Application.platform == RuntimePlatform.IPhonePlayer) || (Application.platform == RuntimePlatform.Android))
{
accel = Input.acceleration * deviceAccelerometerSensitivity;
if(accel.x > deadZone || accel.x < -deadZone)
{
horizontal = accel.x;
}
else
{
horizontal = 0;
}
throttle = 0;
for (var touch : Touch in Input.touches)
{
if(touch.position.x > Screen.width - Screen.width/3 && touch.position.y < Screen.height / 3)
{
throttle = 1;
}
else if (touch.position.x <Screen.width/3 && touch.position.y < Screen.height / 3)
{
throttle = -1;
}
}
}
else if((Application.platform == RuntimePlatform.WindowsEditor) || (Application.platform == RuntimePlatform.WindowsWebPlayer))
{
horizontal = Input.GetAxis("Horizontal");
throttle = Input.GetAxis("Vertical");
}
}
function carPhysicsUpdate()
{
myRight = carTransform.right;
velo = carRigidbody.velocity;
tempVEC = Vector3(velo.x,0,velo.z);
flatVelo = tempVEC;
dir = transform.TransformDirection(carFwd);
tempVEC = Vector3(dir.x,0,dir.z);
flatDir = Vector3.Normalize(tempVEC);
relativeVelocity = carTransform.InverseTransformDirection(flatVelo);
slideSpeed = Vector3.Dot(myRight, flatVelo);
mySpeed = flatVelo.magnitude;
rev = Mathf.Sign(Vector3.Dot(flatVelo,flatDir));
engineForce = (flatDir * (power * throttle) * carMass);
actualTurn = horizontal;
if (rev<0.1f)
{
actualTurn=-actualTurn;
}
turnVec = (((carUp * turnSpeed) * actualTurn) * carMass) * 800;
actualGrip = Mathf.Lerp(100, carGrip, mySpeed *0.02);
imp = myRight*(-slideSpeed * carMass * actualGrip);
}
function slowVelocity()
{
carRigidbody.AddForce(-flatVelo * 0.8);
}
function engineSound()
{
audio.pitch = 0.30 + mySpeed * 0.025;
if(mySpeed>30)
{
audio.pitch = 0.25 + mySpeed * 0.015;
}
if(mySpeed>40)
{
audio.pitch = 0.20 + mySpeed * 0.013;
}
if(mySpeed>50)
{
audio.pitch = 0.15 + mySpeed * 0.011;
}
if(audio.pitch>2.0)
{
audio.pitch = 2.0;
}
}
function fixedUpdate()
{
if(mySpeed < maxSpeed)
{
carRigidbody.AddForce(engineForce*Time.deltaTime);
}
if(mySpeed > maxSpeedToTurn)
{
carRigidbody.AddTorque(turnVec*Time.deltaTime);
}
else if(mySpeed < maxSpeedToTurn)
{
return;
}
carRigidbody.AddForce(imp * Time.deltaTime);
}
and heres the error log
thanks in advance… :)
PS: the code is from this tutorial link text
Answer by Ambro · Feb 10, 2014 at 05:07 PM
carTransform is null because you don't call the Initialize method. You may want to rename it Start or Awake.
I don't know if it's voluntary but fixedUpdate must be written FixedUpdate and you must use Time.fixedDeltaTime inside instead of Time.deltaTime
hi there… thanks a lot for your help… there are no more errors… but now i have a different problem. The car just won't move unless i put in the throttle manually through the keyboard… any suggestions?
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Why is using GetComponent(CharacterController) causing null reference exceptions? 1 Answer
Want to make my object move in a different direction after mouse click. 1 Answer
LoadLevel Health Question. 1 Answer