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 /
  • Help Room /
avatar image
0
Question by Corten · Aug 22, 2015 at 01:02 PM · collidercolliding

OnTriggerExit triggers randomly while still in trigger zone

I have 2 game objects a player and an enemy. The player has a rigidbody and an AttackRange named collider set as trigger assigned to it as a child object and it has a script that detects enemies in range then puts them in a List, if you move out of range it removes it. The enemy has a collider as well set as a trigger. Now my problem is that even though the enemy is in the AttackRange it randomly removes it from the lost for no apparent reason, I've already wasted 2 afternoons on this looking for answers to my problem, but to no avail.

Here is the AttackRange script.

 using UnityEngine;
 using System.Collections.Generic;
 
 public class AttackRange : MonoBehaviour {
 
     public List<GameObject> enemiesInRange = new List<GameObject>();
     float timer;
     
     // Update is called once per frame
     void Update () {
         timer += Time.deltaTime;
     
     }
 
     public void OnTriggerEnter(Collider other){
         if (other.gameObject.tag == "Enemy" && other.isTrigger == true) {
             if(enemiesInRange.Contains(other.gameObject)){
 
             } else {
                 enemiesInRange.Add(other.gameObject);
                 print ("Added: " + other.gameObject + " at " + timer);
             }
 
         }
 
 
     }
 
     public void OnTriggerExit(Collider other){
         if (other.gameObject.tag == "Enemy" && other.isTrigger == true) {
             if(enemiesInRange.Contains(other.gameObject)){
                 enemiesInRange.Remove(other.gameObject);
                 print ("Removed: " + other.gameObject + " at " + timer);
                 timer = 0.0f;
             } else {
 
             }
         }
         
     }
 
     public List<GameObject> getEnemiesInRange(){
         return enemiesInRange;
 
     }
 }
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
3

Answer by HappyGecko · May 10, 2016 at 09:06 PM

Found this thread and I believe I have a similar problem. My solution is to change the Rigidbody setting 'Collision detection' from Discrete to Continuous which fixed it.

My problem was that the enemy would set the trigger just the once and then after 5 seconds or so it would start spamming the trigger each frame as it sat within the collider.

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 angrypenguin · May 08, 2017 at 06:17 AM 0
Share

I just ran into this issue and this workaround was successful for me, too.

$$anonymous$$y scenario: - Large box collider trigger representing an area an object had to be removed from. - A Rigidbody object with a sphere collider that the player pushes out of the area. - The Rigidbody also had a deactivated child object with a trigger collider. Being deactivated this should theoretically have no effect.

Anyhow, on running the scene OnTriggerExit(...) would immediately get called on scripts attached to the Rigidbody's GameObject, even though it (and its children) were still well within the area collider. I checked and confirmed that the trigger it thought it was exiting was the correct one.

After reading the above I then switched the Rigidbody to use Continuous mode, and the problem stopped. Swapping back and forth between Continuous and Discreet reliably causes the problem to return or stop.

This is in Unity 5.5.0f3.

avatar image Ylor · Mar 17, 2018 at 03:17 PM 0
Share

O$$anonymous$$G! I searched my code for errors for hours because the triggerZone registered a lot of objects entering one frame, only to register them as exited in the next (although they were still there). In the end, this solved my problem. I can't understand how this bug uccours in the first place, though! Anyway, thank you!

EDIT: Appearently it wasn't it. It worked randomly once after I changed, but now the problem is back, even with continous collision detection.

avatar image
1

Answer by Prophete · Apr 12, 2020 at 03:45 PM

In lot of case problem with collision come with the type of collision you are using (Static Collider, Kinematic Rigidbody Trigger Collider, etc.) This talk about compatibility with each ones : https://docs.unity3d.com/Manual/CollidersOverview.html

For your specific issue, for example, when using a Kinematic Rigidbody Trigger Collider on one GameObject, the other one could be a Static Collider (the GameObject should not have a Rigidbody) and the problem of random exit when still inside the wanted collider will disapear.

Hope it help some people.

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

30 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

Related Questions

Player cannot pass through object (Please help me) 2 Answers

onmouse over UI pop up help please 0 Answers

My collider2D don't work 1 Answer

Im having trouble figuring out a way to make these two functions work. 1 Answer

How to check if gameobject has a tag, from a list. 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