Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 sorozco0612 · Jul 05, 2017 at 08:13 AM · collision detectioncollisions

Why is OnTriggerStay is not working?

Hey everybody!

So I am having a problem with OnTriggerStay. I have two game objects. The first game object is my player. He has a Trigger Capsule Collider component around his entire body and a Character Controller component. The second game object is my enemy and has a RigidBody and a Trigger Capsule Collider. I am trying to get the enemy to harm the player when the player enters the enemy's trigger. For the most part, OnTriggerStay works and my enemy does harm to the player, but every now and then, the enemy does zero harm to the player even when the player is inside the enemy's trigger. Here is the code:

 void OnTriggerStay ( Collider other) {
             GameObject hitObject = other.transform.gameObject;
             PlayerHealth target = hitObject.GetComponent<PlayerHealth> ();
             if (target == null) {
                 return;
             }
             target.TakeDamage (10);
     }

The problem is that sometimes the 'target' variable is set to null because the enemy cannot access the player's 'PlayerHealth' script even though he is inside the trigger. I'm just really confused as to why it works only most of the time. Do you have any suggestions?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Vollmondum · Jul 05, 2017 at 08:19 AM

Replace OnTriggerStay to OnTriggerEnter and Exit. Say OnEnter, your Players script enabled some bool "takeDamage" that invokesRepeating of taking constant damage. TriggerExit sets bool to false. You'll save some on performance.

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 sorozco0612 · Jul 05, 2017 at 08:49 AM 0
Share

That worked! I'm not entirely sure why it worked, but I haven't encountered any problems yet. Thank you!

avatar image Vollmondum sorozco0612 · Jul 05, 2017 at 08:57 AM 0
Share

TriggerStay is basically alternative Update function. You don't want that for over 1-2 seconds

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

66 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Stop Continuous Collision with GameObject 1 Answer

How to get rid of jumpy collisions? 1 Answer

How to do static collision checks? 1 Answer

detecting collision of collided object 2d 1 Answer

OnTriggerEnter called much less often after a player respawn? 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