- Home /
This question was
closed Sep 18, 2014 at 02:44 PM by
Graham-Dunnett for the following reason:
Duplicate Question - there are over 50 pages of questions about the same thing
Question by
david88888 · Sep 18, 2014 at 02:41 PM ·
javascriptnullreferenceexceptionontriggerenter
Object reference not set to an instance of an object, How can I fix it?
I follow the tutorial to make a game, and I done exactly with the tutorial. However some errors occur. When the player on trigger the coin, the coin can't destroy and the particle also not occur. How can I fix it. NullReferenceException: Object reference not set to an instance of an object ItemPickup+$OnTriggerEnter$17+$.MoveNext () (at Assets/Scripts/ItemPickup.js:34)
enum PickupType
{
Grow = 0,
Key = 1,
Coin = 2,
Fireball = 3,
ExtraLife = 4,
GameTime = 5
}
var pickupType = PickupType.Grow;
var pickupValue : int = 1;
var itemParticle : Transform;
var soundItemPickup : AudioClip;
var soundDelay : float = 0.0;
var soundRate : float = 0.0;
private var playerGameObject : GameObject;
private var hudGameObject : GameObject;
private var extraLifeEnabled : boolean = false;
function Start()
{
playerGameObject = GameObject.FindWithTag("Player");
hudGameObject = GameObject.FindWithTag("hud");
}
function OnTriggerEnter(other : Collider)
{
if ( other.tag == "collisionBoxBody" )
{
var pProp = playerGameObject.GetComponent(PlayerProperties);
ApplyPickup( pProp );
renderer.enabled = false;
if (itemParticle)
{
Instantiate(itemParticle, transform.position, transform.rotation);
}
if (soundItemPickup)
{
PlaySound (soundItemPickup, 0);
}
yield WaitForSeconds(audio.clip.length);
if (extraLifeEnabled)
{
pProp.lives += pickupValue;
extraLifeEnabled = false;
}
Destroy (other.gameObject);
}
}
function ApplyPickup(playerStatus : PlayerProperties)
{
var hudConnect = hudGameObject.GetComponent(HudController);
switch (pickupType)
{
case PickupType.Grow:
if (playerStatus.playerState != PlayerState.MarioFire)
{
playerStatus.playerState = PlayerState.MarioLarge;
playerStatus.changeMario = true;
}
break;
case PickupType.Key:
playerStatus.AddKeys (pickupValue);
break;
case PickupType.Coin:
playerStatus.AddCoin (pickupValue);
hudConnect.coin += pickupValue;
break;
case PickupType.Fireball:
playerStatus.playerState = PlayerState.MarioFire;
playerStatus.hasFire = true;
playerStatus.changeMario = true;
break;
case PickupType.ExtraLife:
extraLifeEnabled = true;
break;
case PickupType.GameTime:
// playerStatus.AddTime(pickupValue);
break;
}
}
function PlaySound(soundName : AudioClip, soundDelay : float)
{
if (!audio.isPlaying && Time.time > soundRate)
{
soundRate = Time.time + soundDelay;
audio.clip = soundName;
audio.Play();
}
}
Comment
did you check that playerGameObject
is not null?
try adding some debug statements