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 Eugenius · Jul 09, 2013 at 09:35 PM · javascriptcollisioncollidertrigger

Trigger Enter and Exit not working properly?

Update: I have tried doing it all over again, this is still happening. Any other ideas?

Hello,

As an awful way to double check for a collision, I've created 4 colliders(divided in 4 directions) attached to an empty gameobject.

Each of the object turns a boolean from the empty gameobject script - true with OnTriggerEnter and false OnTriggerExit.

When all of them are true something (not sure what yet) is going to happen.

The problem is the following: while my AI is moving (so are the colliders) and if they move quickly they cancel each other somehow that they are unable to be all true although all of them are colliding with the triggers.

Here is an example of the code I use on one of the colliders:

 function OnTriggerEnter(hit:Collider)
 {
     if(hit.collider.tag == "Fence")
     {
         var gameObject = GameObject.FindGameObjectWithTag("DoubleCheck");
         var script = gameObject.GetComponent(DoubleCheckAI);
         script.topDcheck = true;
     }    
 }
 
 function OnTriggerExit(hit:Collider)
 {
     var gameObject = GameObject.FindGameObjectWithTag("DoubleCheck");
     var script = gameObject.GetComponent(DoubleCheckAI);
     script.topDcheck = false;
 }

Notes: I have kinematic rigidbodies attached on the colliders. I have the colliders set to isTrigger.

Any help on this would be greatly appreciated because I've never encountered this until now...

EDIT (Scenario information): Certainly.

My AI is moving constantly (currently the AI is a sphere with a sphere collider).

Attached to my AI I have an empty game object with a cube collider to count the number of fences. Also attached to the AI I am doing a double check of the collision using 4 box colliders (for 4 directions) - these are the problem.

The fences are prefabs instantiated on click + drag in a certain direction.

The idea of the game is to capture the AI between 4 fences - that is what isn't working for me at all times.

Due to the issue with the double check I am not always seeing that the AI is captured if it moves. If my AI would be static if wouldn't be a problem as I've tried moving the colliders manually while pausing the unity player.

Not sure what to do next...

Comment
Add comment · Show 4
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 amphoterik · Jul 10, 2013 at 11:30 AM 0
Share

It is worth noting that you would be far better off to find and store the script component you are using in the Start() method. Doing this every collision will eat up performance.

avatar image Eugenius · Jul 10, 2013 at 11:32 AM 0
Share

Yes I am aware of that, I just wrote a mock-up code to see if my double check system works and surprise surprise this is happening to me :). Could it be a Unity Bug?

avatar image amphoterik · Jul 10, 2013 at 11:32 AM 0
Share

Can you better describe your scenario? What objects are moving? What is this script attached to? What is "fence"? Your code looks fine, so the problem is with the interaction.

avatar image Eugenius · Jul 10, 2013 at 11:39 AM 0
Share

Added after Edit, hope that information clarifies things better :). Also, I know that my method is messy but it's the most effective for what I'm trying to achieve.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by theAfrican · Jul 09, 2013 at 09:47 PM

looks like you're overriding gameObject with var gameObject. rename it to var gObject and try again. gameObject is a reserved name you shouldnt override it.

try

 function OnTriggerEnter(hit:Collider)
 {
     if(hit.collider.tag == "Fence")
     {
        var gObject =  GameObject.FindGameObjectsWithTag("DoubleCheck");
        if (gObject==null) return;
        for (var n in gObject)
            {
            var script = n.GetComponent(DoubleCheckAI);
            script.topDcheck = true;
            }
     }  
 }
  
 function OnTriggerExit(hit:Collider)
 {
     var gObject =  GameObject.FindGameObjectsWithTag("DoubleCheck");
        if (gObject==null) return;
        for (var n in gObject)
           {
           var script = n.GetComponent(DoubleCheckAI);
           script.topDcheck = false;
           }
 }
Comment
Add comment · Show 3 · 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 Eugenius · Jul 09, 2013 at 09:56 PM 0
Share

Sorry mate, does not help. The issue still happens...

  • for the initiative anyway, thanks :)

avatar image theAfrican · Jul 09, 2013 at 10:13 PM 0
Share

did u mean GameObject.FindGameObjectsWithTag()?, urs is GameObject.FindGameObjectWithTag Description Returns a list of active GameObjects tagged tag. Returns null if no GameObject was found. so, shouldnt you be looping through that?

just edited my answer, try it out

avatar image Eugenius · Jul 10, 2013 at 07:14 AM 0
Share

No, i know the difference between object and object*s*. I am finding the object that contains the script. No need to find multiple objects as there is only one with the "DoubleCheck" tag in the whole scene.

avatar image
0

Answer by MrPhil · Apr 17, 2016 at 08:35 PM

Two ideas:

1) Any chance you've mixed up the 3D and 2D version of these methods and components? I do that all the time.

  • Rigidbody Vs Rigidbody2D

  • Collider.OnTriggerEnter(Collider) Vs Collider2D.OnTriggerEnter2D(Collider2D)

  • Collider.OnCollisionEnter(Collision) Vs Collider2D.OnCollisionEnter2D(Collision2D)

  • BoxCollider Vs BoxCollider2D

etc.

2) Another thing that happens is you have the OnCollisionEnter2D setup in code, but you set the BoxCollider2D to be a Trigger, so you should be using the OnTriggerEnter2D in code.

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

18 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

Related Questions

How to make collision check if other collider has component 1 Answer

Using a Single Collider to Detect Collisions with Multiple Colliders 3 Answers

Check if trigger collider is touching other trigger collider 2 Answers

Find colliders after collision? 2 Answers

Having problems with 2d collision triggers (javascript) 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