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