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 Mr_Happy_Pixel · Jan 06, 2018 at 05:10 PM · collidertriggerontriggerentertriggers

OnTrigerEnter not working

hello fellow unity devs,

I have been incountering a problom with onTriggerEnter ,here is my c# script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class causeSwiming : MonoBehaviour {
     public bool swiming;
     public bool swimingInFrame;
     public List<GameObject> TouchingObjects;
     void Start()
     {
         TouchingObjects = new List<GameObject>();
     }
 
     void OnTriggerEnter(Collider collision)
     {
         if (TouchingObjects.Contains(collision.gameObject) == false)
         {
             TouchingObjects.Add(collision.gameObject);
         }
     }
 
     void OnTriggerExit(Collider collision)
     {
         if (TouchingObjects.Contains(collision.gameObject) == true)
         {
             TouchingObjects.Remove(collision.gameObject);
         }
     }
 
     // Update is called once per frame
     void Update () {
         swimingInFrame = false;
         for (int i = 0; i < TouchingObjects.Count; i ++)
         {
             if (TouchingObjects[i].CompareTag("water") == true)
             {
                 swimingInFrame = true;
             }
         }
         if (swimingInFrame == true)
         {
             swiming = true;
         }else
         {
             swiming = false;
         }
     }
 }
 

The list TouchingObjects should contain all game objects with triggers touching the trigger of the object witch the script is a conponent of.But it has nothing in it.
sorry for any bad spelling.
thanks in advence.

Comment
Add comment · Show 6
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 pako · Jan 06, 2018 at 06:08 PM 0
Share

The list TouchingObjects should contain all game objects with triggers touching the trigger of the object witch the script is a conponent of.But it has nothing in it

How come?

Whatever GameObject you add to the TouchingObjects list after OnTriggerEnter you remove after OnTriggerExit! So, how do you expect anything to remain in the list?

avatar image Mr_Happy_Pixel · Jan 06, 2018 at 08:04 PM 0
Share

@pako , but only after it exits

avatar image pako Mr_Happy_Pixel · Jan 06, 2018 at 08:12 PM 0
Share

So, you're saying that before exiting there are no GameObjects added to the list? How do you verify that. Do you use a debugger and check the contents of the List or what?

avatar image Mr_Happy_Pixel pako · Jan 06, 2018 at 09:39 PM 0
Share

@pako no,I test the game in the game window and check the inspector to look at the list.

Show more comments

2 Replies

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

Answer by FlaSh-G · Jan 06, 2018 at 08:11 PM

Do all your objects you expect to be in the list have a collider?

Is it 3D colliders? 2D colliders won't trigger these events, but their 2D counterparts.

Does the object this script is on have a proper 3D collider that is set to "Is Trigger" as well?

Are all colliders MeshColliders? If yes, they won't collide with each other.

Edit: Does one or more of the involved objects have a rigidbody component? They're the ones that trigger these events.

Comment
Add comment · Show 9 · 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 Mr_Happy_Pixel · Jan 06, 2018 at 09:42 PM 0
Share

@FlaSh-G all objects have 3d box colliders with is trigger checked.

avatar image FlaSh-G · Jan 06, 2018 at 10:59 PM 0
Share

Oh... another question: Do your boxes have Rigidbodies? These events are called by Rigidbodies, not Colliders.

avatar image Mr_Happy_Pixel FlaSh-G · Jan 06, 2018 at 11:27 PM 0
Share

@FlaSh-G nope , none of them have rigidbodies.So one of them needs a rigidbody , darn.

avatar image FlaSh-G · Jan 06, 2018 at 11:28 PM 0
Share

Yeah... Colliders are data objects, they don't do anything by themselves. A rigidbody moves its GameObject around and checks if it has collided with something.

avatar image Mr_Happy_Pixel FlaSh-G · Jan 07, 2018 at 12:44 AM 0
Share

@FlaSh-G shoot, does a rigidbody with a trigger collider do physics calculations?

avatar image FlaSh-G · Jan 07, 2018 at 01:09 PM 0
Share

Yes, but you should usually only put rigidbody components on objects if you plan on moving them around with them. So... consider putting the RBs on the moving objects rather than the (probably static?) trigger.

avatar image Mr_Happy_Pixel FlaSh-G · Jan 07, 2018 at 01:46 PM 0
Share

Well both objects are (for the most part) static , one is a camera and one is a body of water , the camera needs to use it's trigger to detect of it is under water so I can activate an UI element.So neither needs a rigidbody for anything else, but I will put it on the camera so I only need one rigidbody that can detect all body's of water. Thank you very much good sir. Can you post these findings as an answer so I can accept it.

avatar image FlaSh-G · Jan 07, 2018 at 01:49 PM 0
Share

I edited my answer :)

Consider not using the physics engine for your problem though. Do you have multiple water levels in your scene? Do you have air-filled tunnels underwater? If you can answer both questions with "no", you could just check the camera's y position ins$$anonymous$$d.

avatar image Mr_Happy_Pixel FlaSh-G · Jan 07, 2018 at 03:29 PM 0
Share

@FlaSh-G I plan on having many water levels and no swim zones ,so I can't just check the players y position ,the thought had come to $$anonymous$$d though.

avatar image
0

Answer by getyour411 · Jan 06, 2018 at 08:13 PM

Insert a line at the top of your OnTriggerEnter:

 Debug.Log(collision.gameObject.name);

This type of problem is usually easily solved by looking at debug output; perhaps you aren't meeting the criteria for an OnTriggerEnter: https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

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 Mr_Happy_Pixel · Jan 06, 2018 at 09:54 PM -1
Share

@getyour411 it meats all the criteria.

avatar image getyour411 · Jan 06, 2018 at 10:09 PM 0
Share

Show the debug.log output

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

103 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 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

Calling "OnTriggerEnter" when a parent object has a rigidbody 0 Answers

OnTriggerEnter is not called 1 Answer

C# OnTriggerEnter Issue 3 Answers

OnTriggerExit2D doesn't work when one of gameObject setActive false. 0 Answers

Enemy Trigger Colliders are triggering my player's trigger collider. Why? 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