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*.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.

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.

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: Unity - Scripting API: Collider.OnTriggerEnter(Collider)