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 PolarBear180 · Jun 08, 2015 at 07:06 PM · triggerboolifelse

OnTriggerEnter if else

Hello guys, having a little issue with a simple script.

I have it set so once a gameObject with the Tag "enemy" enters a Trigger it changes its tag to another tag and triggers a bool that triggers an if statement. The bool is then changed back once the gameObject has exit the trigger.

But if the gameObject is destroyed while inside of the trigger area then the bool can't change back so I added an else but it stops the whole script from working? As the bool does not trigger even if the gameObject enters.

 void OnTriggerStay(Collider col) {
     if (col.gameObject.tag == "enemy") {
         aggro = true;
     } else {
         aggro = false;
     }
 }

 void OnTriggerExit(Collider colExit) {
     if (colExit.gameObject.tag == "enemy") {
         aggro = false;
     }
 }


So how would I change the bool back to false once and if the enemy gameObject is destroyed while in the trigger area?

Hope this makes sense!

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

Answer by AlwaysSunny · Jun 08, 2015 at 07:06 PM

Normally this is the kind of question I would answer-and-reject, but I'm pretty curious to hear any feedback from programming vets who might be able to teach me something new about handling these situations.

Seems like this might be your first time experiencing the shortcomings of event-based logic. It's a rather complicated discussion, honestly: planning for every contingency while also writing clean and inexpensive logic.

If you've got some logic that needs to take place when some event (like a collision response or input event) triggers, and the object that responds to this event needs to also respond to another subsequent event (button released, exit trigger, etc), there are many possible activities that can interrupt this from happening.

Say the user presses a key, this fires a 'start' event, then the game or script loses focus before the key is released to fire the 'end' event. Or the object you're manipulating gets destroyed or changed in some way that invalidates the subsequent event you're waiting for.

These issues have driven me to stop relying on events in this fashion. Rather than plan for every possible contingency, I choose a different - more expensive but more reliable - style of handling such things.

Any time I need a "start" and "end" event to change something about one object, and there exists the potential that the end event will fail, I do not use an "end" event at all, or, I treat that end event as one of several possible outcomes.

Often this involves using OnTriggerStay instead of Enter/Exit. Often it involves using OnButton instead of Down/Up. In other words, it involves checking the status said event would normally check for me every frame. This organizational method gives you the opportunity to check for other conditions which might invalidate what would normally be your "end" event.

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
avatar image
0

Answer by tanoshimi · Jun 08, 2015 at 07:31 PM

I wouldn't personally use @AlwaysSunny's suggestion of OnTriggerStay() - I'd continue to use Enter/Exit but also maintain a list of what's currently in the trigger area - if you want you can then check that list every frame if you want in Update (which would provide similar functionality to OnTriggerStay), or check it less frequently via a coroutine, or at any specific time you want:

https://alastaira.wordpress.com/2013/12/12/keeping-track-of-targets-in-a-trigger/

In your case, if the list is ever empty (either because all gameobjects left the trigger or because they were destroyed), you set aggro to false.

Comment
Add comment · Show 1 · 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 AlwaysSunny · Jun 08, 2015 at 07:36 PM 0
Share

Oh yeah, I didn't elaborate that far, did I? Good on ya! I've used similar strategies in the past. Guess I asked wanting to make absolutely sure I wasn't missing some kind of magic that would make these problems disappear.

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

Keycode else 1 Answer

I am getting error cs1525 for '}' on line 11,17. How come? 1 Answer

Void OnTriggerEnter not working 1 Answer

BCE0044 Error in the Start function 1 Answer

problem with instance 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