- Home /
Null Reference Exception Error
I am adding to the Car Tutorial a particle system that is using the following script. If you take note the script is actually modified from the Pickup script from the 3D Platform Game. I want it so that when the car collides with the particle system, the top speed is increased. This script is added to the particle system. When I run the program, I am getting an error:
NullReferenceException: Object reference not set to an instance of an object UpdateTopSpeed.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Scripts/JavaScripts/UpdateTopSpeed.js:41)
Does anyone know how to fix this error or how to change the code around so that I don't have to worry about the error. Note I have not changed much from the tutorial besides adding the particle system and creating this script called UpdateSpeed.
Thank you!
enum PickupType { Health = 0, FuelCell = 1 , TimeCell = 2}
var pickupType = PickupType.TimeCell;
var amount = 1;
var sound : AudioClip;
var soundVolume : float = 2.0;
var carStatus : Car;
private var used = false;
private var mover : DroppableMover;
function Start ()
{
// do we exist in the level or are we instantiated by an enemy dying?
mover = GetComponent(DroppableMover);
}
function ApplyPickup (carStatus : Car)
{
// A switch...case statement may seem overkill for this, but it makes adding new pickup types trivial.
switch (pickupType)
{
case PickupType.Health:
carStatus.AddHealth(amount);
break;
case PickupType.FuelCell:
carStatus.FoundItem(amount);
break;
case PickupType.TimeCell:
carStatus.FoundItem(amount);
}
return true;
}
function OnTriggerEnter (col : Collider) {
if(mover && mover.enabled) return;
var carStatus : Car = col.GetComponent(Car);
carStatus.GetComponent(Car).UpdateSpeed();
//* Make sure we are running into a player
//* prevent picking up the trigger twice, because destruction
// might be delayed until the animation has finished
if (used || carStatus == null)
return;
if (!ApplyPickup (carStatus))
return;
used = true;
// Play sound
if (sound)
AudioSource.PlayClipAtPoint(sound, transform.position, soundVolume);
// If there is an animation attached.
// Play it.
if (animation && animation.clip)
{
animation.Play();
Destroy(gameObject, animation.clip.length);
}
else
{
Destroy(gameObject);
}
}
// Auto setup the pickup
function Reset ()
{
if (collider == null)
gameObject.AddComponent(BoxCollider);
collider.isTrigger = true;
}
I have established that the error is within these two lines:
var carStatus : Car = col.GetComponent(Car); carStatus.GetComponent(Car).UpdateSpeed();
Wait it's not a variable yet... You need to make it a variable b4 anything. Null means nothing. Referance means it cannot refer to something, error means failure to do... umm and the object must be a trigger/ have trigger flagged/ have trigger thing checked ont he collider.
Answer by aldonaletto · May 05, 2012 at 12:58 AM
If Car is a script and UpdateSpeed() a function of it, you could use this:
... var carStatus : Car = col.GetComponent(Car); carStatus.UpdateSpeed(); ...or just this:
... col.GetComponent(Car).UpdateSpeed(); ...
This didn't work. I am still getting the same error.
ALDO DO $$anonymous$$Y QUESTION ON THE UNANSWERED QUESTION PAGES! He doesnt have it as it... $$anonymous$$aybe you should try another script. Perhaps make your own?
Probably GetComponent have not found the script Car in the object that entered the trigger, or the function UpdateSpeed doesn't exist/is misspelled. The object may not be the car, or the script Car isn't attached to it. You should use some print instructions to help finding the cause:
... if(mover && mover.enabled) return; var carStatus : Car = col.GetComponent(Car); if (carStatus){ carStatus.UpdateSpeed(); } else { print("Script Car not found in "+col.name); } ...If the object detected doesn't have the script Car, this will print a warning and the name of this object.