Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by multinfs · Jun 04, 2012 at 04:19 PM · ontriggerenterontriggerexitontriggerstaycollisiondetection

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best 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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · Jun 05, 2012 at 05:12 PM 0
Share

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();
avatar image Berenger · Jun 06, 2012 at 02:52 PM 0
Share

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.

avatar image
0

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();
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges