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.
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?
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?
@pako no,I test the game in the game window and check the inspector to look at the list.
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.
@FlaSh-G all objects have 3d box colliders with is trigger checked.
Oh... another question: Do your boxes have Rigidbodies? These events are called by Rigidbodies, not Colliders.
@FlaSh-G nope , none of them have rigidbodies.So one of them needs a rigidbody , darn.
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.
@FlaSh-G shoot, does a rigidbody with a trigger collider do physics calculations?
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.
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.
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.
@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.
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
Your answer
Follow this Question
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