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:
         case PickupType.FuelCell:
         case PickupType.TimeCell:
     return true;
 function OnTriggerEnter (col : Collider) {
     if(mover && mover.enabled) return;
     var carStatus : Car = col.GetComponent(Car);
     //* 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)
     if (!ApplyPickup (carStatus))
     used = true;
     // Play sound
     if (sound)
         AudioSource.PlayClipAtPoint(sound, transform.position, soundVolume);
     // If there is an animation attached.
     // Play it.
     if (animation && animation.clip)
         Destroy(gameObject, animation.clip.length);
 // Auto setup the pickup
 function Reset ()
     if (collider == null)    
     collider.isTrigger = true;
more ▼

asked May 05, 2012 at 12:34 AM

unityquestions12345 gravatar image

1 3 2 2

I have established that the error is within these two lines:

var carStatus : Car = col.GetComponent(Car); carStatus.GetComponent(Car).UpdateSpeed();

May 05, 2012 at 12:43 AM unityquestions12345

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.

May 07, 2012 at 12:30 AM maroonrs2
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

If Car is a script and UpdateSpeed() a function of it, you could use this:

    var carStatus : Car = col.GetComponent(Car);
or just this:

more ▼

answered May 05, 2012 at 12:58 AM

aldonaletto gravatar image

57.7k 33 94 305

This didn't work. I am still getting the same error.

May 07, 2012 at 12:18 AM unityquestions12345

ALDO DO MY QUESTION ON THE UNANSWERED QUESTION PAGES! He doesnt have it as it... Maybe you should try another script. Perhaps make your own?

May 07, 2012 at 12:27 AM maroonrs2

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){
    } 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.
May 07, 2012 at 02:15 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: May 05, 2012 at 12:34 AM

Seen: 1681 times

Last Updated: May 07, 2012 at 02:15 PM