- Home /
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 :)
Answer by Berenger · Jun 04, 2012 at 04:20 PM
Check if ammo's gameObject have a an AudioSource. Also, GetComponent is accessible from any components, you don't need the gameObject for that.
multin fs : it does have the audiosource but shouldnt it still work with .gameObject. even tho it isnt needed?
$$anonymous$$e : Yes, it's gonna work. So is ammo.gameObject.gameObject.gameObject.gameObject.GetComponent(AudioSource).Play();, but it's redundant.
Your problem is that ammo is null, or gameObject or GetComponent. ammo.pickupSound doesn't raise an exception, so this one is ok. No reason for gameObject to be null, that leave the GetComponent. Check it like that :
AudioSource as = ammo.GetComponent(AudioSource); // Same as ammo.audio, btw
if( as == null ) print( ammo + " doesn't have an AudioSource" );
else as.Play();
multinfs : Well I tried with that but it just says that it doesnt have any audiosource but thats when Im not even close to the ammobox, its something wrong with the collider cuz it shouldnt think that every object I touch have the tag ammopickup when they havent.
I stand on the floor whitch havent got any tag, And I got this line pretty much in the start of the function OnTriggerStay:
if(collision.tag == "AmmoPickup" && waitToPickupAmmo
But still it passes and says that the ammo object which is now the floor, doesnt have any audiosoucre. And it shouldnt have one either cuz its the ammobox that has it.
I just dont know anymore how to fix this, its looking like a bug to me -.-
$$anonymous$$e : Use print() to find out what's going on. If the tag isn't AmmoPickup and it still pass, something is seriously wrong.
Answer by multinfs · Jun 08, 2012 at 01:46 PM
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();
Your answer
Follow this Question
Related Questions
OntriggerEnter / Stay with the same Gameobject tags 0 Answers
Problem with OnTriggerEnter 1 Answer
OnTriggerExit is not calling 2 Answers
OnTriggerEnter only working once. 0 Answers
Can I access a script OnTrigger WITHOUT using getcomponent? 1 Answer