OnTriggerStay/Enter/Exit

I got a problem with the OnTriggerStay function, I have tried using OnTriggerEnter/Exit but problems there too. What I’m trying to do is I have a ammobox on the floor and I hold a gun that should pick upp ammo from that ammobox when they collide.

The problem is that when I use OnTriggerStay I get an error saying
“NullReferenceException: Object reference not set to an instance of an object PlatformerController.OnTriggerStay (UnityEngine.Collider collision) (at Assets/Standard Assets/Scripts/Platformer Shooter Scripts/PlatformerController.js:126)”

Code:
function OnTriggerStay(collision : Collider) {

var current : RayShoot = null;
if(currentGun)
	current = currentGun.gameObject.GetComponent(RayShoot);
var ammo : AmmoPickupScript = null;
var gun : RayShoot = null;
if(collision.tag == "AmmoPickup" && waitToPickupAmmo <= 0){
	ammo = collision.gameObject.GetComponent(AmmoPickupScript);
	if(ammo.pickupSound)
		ammo.gameObject.GetComponent(AudioSource).Play();          <----Line 126
	if(current.currentExtraAmmo < current.maxExtraAmmo){
		if(ammo.fromGun){
			gun = ammo.gun.GetComponent(RayShoot);
			if(gun.currentExtraAmmo > 0 && gun.ammoType == current.ammoType && ammo.gun != currentGun){
				if(gun.currentExtraAmmo >= current.maxExtraAmmo - current.currentExtraAmmo){
					gun.currentExtraAmmo -= current.maxExtraAmmo - current.currentExtraAmmo;
					current.currentExtraAmmo = current.maxExtraAmmo;
				}
				if(gun.currentExtraAmmo < current.maxExtraAmmo - current.currentExtraAmmo){
					current.currentExtraAmmo += gun.currentExtraAmmo;
					gun.currentExtraAmmo = 0;
				}
			}
		}
		if(!ammo.fromGun){
			if(current.ammoType == ammo.ammoType || ammo.ammoType == -1){
				if(ammo.ammoAmount > 0 || !ammo.unlimitedAmmo){
					if(ammo.ammoAmount >= current.maxExtraAmmo - current.currentExtraAmmo){
						gun.currentExtraAmmo -= current.maxExtraAmmo - current.currentExtraAmmo;
						current.currentExtraAmmo = current.maxExtraAmmo;
					}
					if(ammo.ammoAmount < current.maxExtraAmmo - current.currentExtraAmmo){
						current.currentExtraAmmo += ammo.ammoAmount;
						ammo.ammoAmount = 0;
					}
				}
				if(ammo.unlimitedAmmo){
					current.currentExtraAmmo = current.maxExtraAmmo;
					if(ammo.pickupSound)
						ammo.gameObject.GetComponent(AudioSource).Play();
				}
			}
		}
	}
}

When I use OnTriggerEnter and have if(col.tag == “AmmoPickup”) it will go through it even tho I’m not close to an object with the tag AmmoPickup.

I have searched this problem for a few days now and I would like to get an answer :slight_smile:

Check if ammo’s gameObject have a an AudioSource. Also, GetComponent is accessible from any components, you don’t need the gameObject for that.

I finally got this thing to work after a long time, I’m not completly sure what I did to fix it but I think the fix was these lines:

var current : RayShoot = currentGun.gameObject.GetComponent(RayShoot);
-----And
var audios : AudioSource = ammo.GetComponent(AudioSource);
if(ammo.pickupSound)
if( audios == null ) print( ammo + " doesn't have an AudioSource" );
else audios.Play();