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 state550 · Sep 12, 2016 at 06:15 AM · colliderdeathontriggerexitdisabledbest practice

What's the best workaround for disabled colliders not firing OnTriggerExit events?

I realize that disabled colliders do not trigger OnTriggerExit.

This is an issue for me because when the main character in my game dies, his collider is disabled until he respawns. Another case is with object pooling where a slain enemy is disabled, but if it was in a collider that was tracking it and it respawns elsewhere, the object it was in will still think it is in there.

So far my workaround has been having objects such as the player or enemies keeping a list of functions that holds all of the OnTriggerExit methods of colliders they are in, and having all of those functions called and the list cleared upon their deaths/disabled state.

Another possible technique I've considered is instead of disabling the collider right away, maybe I could move it to a space way outside of the rest of the game world and then disable it after the next FixedUpdate, to ensure that OnTriggerExit is called first. This seems kind of hacky to me but looks much better in the code than the workaround I mentioned above.

Does anyone have any better ideas of how to deal with this issue? Maybe I am missing a more elegant solution. I'd love to hear your input.

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 etopsirhc · Sep 12, 2016 at 06:20 AM

simplest solution would be to add void OnDisable() to the object you want to disable, then force it to call OnTriggerExit on itself so it does exit before it gets disabled.

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 state550 · Sep 12, 2016 at 07:57 PM 1
Share

I don't believe this would solve the problem. Let's say that the object we want to disable is touching some other objects. When the object is disabled, we want those other objects to realize it is no longer inside of them, and call their OnTriggerExit methods. So the issue is not resolved by the disabled object calling its own OnTriggerExit function, but ins$$anonymous$$d calling the OnTriggerExit functions of the objects it is touching. I'm able to work around this as I mentioned above, but I was looking for a cleaner method.

avatar image
0

Answer by Masterio · Sep 12, 2016 at 08:20 AM

[1.] Personally i am using this solution (i've hope it is clear):

I have variable in player game logic:

 _isDead = false;

if it is true then scripts stops , if i place in code 'if statement' like the:

 if(Player.isDead)
    {return;}

It stops executing all AI on dead player body.

[2.] If you want to use interfaces you can create one for this task.

. . .

 public interface IPlayerRespawn
 {
     void OnPlayerDie();
 }

next i wrote example classes (added some debug functions you can check how it works on kill):

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Player_Test : MonoBehaviour 
 {
     private static List<IPlayerRespawn> _iPlayerRespawns = new List<IPlayerRespawn>();
 
     #if UNITY_EDITOR 
     public bool _debugKill = false;
 
     void Update()
     {
         if(_debugKill)
         {
             _debugKill = false;
             Kill();
         }
     }
     #endif
 
     void Kill()
     {
 
         ExecuteIPlayerRespawn();
     }
 
     /// <summary>
     /// Registers the IPlayerRespawn implementation.
     /// </summary>
     public static void RegisterIPlayerRespawn(IPlayerRespawn iPlayerRespawn)
     {
         _iPlayerRespawns.Add(iPlayerRespawn);
     }
 
     // Executes all registered instances.
     private static void ExecuteIPlayerRespawn()
     {
         foreach(IPlayerRespawn i in _iPlayerRespawns)
         {
             i.OnPlayerDie();
         }
     }
 }

and example enemy class:

 using UnityEngine;
 using System.Collections;
 
 public class Enemy_Test : MonoBehaviour, IPlayerRespawn
 {
     void Start()
     {
         // just place it in start or awake
         Player_Test.RegisterIPlayerRespawn(this);
     }
 
     private void SomeMethodToExecute()
     {
         Debug.Log("Enemy "+gameObject.name+" executed.");
     }
 
     #region IPlayerRespawn implementation
     public void OnPlayerDie()
     {
         SomeMethodToExecute();

             // you can add (f.e.) back to spawn position, recover hp etc.
     }
     #endregion
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Option to Toggle Renderers/Colliders OFF Slows Game FPS to Standstill 1 Answer

How do i stop an asset from deleting the ground when it dies? 0 Answers

Strange OnTriggerEnter/Exit() Behavior 0 Answers

OnTriggerExit is not calling 2 Answers

Help with InstantDeath Collider Script? 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