[Solved] When OnTriggerEnter() OnTriggerStay(), and OnTriggerExit() starts firing, stops afterwards.

Sorry about the scale of the images. I don’t know how to scale them down.

In this screenshot, I have shown the game object with a sphere collider attached as a child game object. (First screenshot). The parent game object looks like a simple capsule (Lower right of the second screenshot).

This is what the Capsule game object has in the Inspector.

Here’s the script code for Line of Sight sphere collider:

using UnityEngine;
using System.Collections.Generic;

public class LineOfSight : MonoBehaviour {
    public List<GameUnit> enemiesInRange = new List<GameUnit>();

    public void OnTriggerEnter(Collider other) {
        Debug.Log("Trigger Enter has fired.");
        GameUnit unit = other.GetComponent<GameUnit>();
        if (unit != null && !unit.hasAuthority && !this.enemiesInRange.Contains(unit)) {
            this.enemiesInRange.Add(unit);
        }
    }

    public void OnTriggerExit(Collider other) {
        Debug.Log("Trigger Exit has fired.");
        GameUnit unit = other.GetComponent<GameUnit>();
        if (unit != null && !unit.hasAuthority && this.enemiesInRange.Contains(unit)) {
            this.enemiesInRange.Remove(unit);
        }
    }

    public void OnTriggerStay(Collider other) {
        Debug.Log("Trigger Stay has fired.");
    }
}

And this is what happens.

When the units are created, you can see the Console logging messages saying the OnTriggerEnter(), OnTriggerStay(), and OnTriggerExit() all firing, with OnTriggerStay() continuously firing.

It’s when after the unit has exited the sphere trigger and goes back into the sphere trigger, the script no longer fires the OnTriggerEnter(), OnTriggerStay(), and OnTriggerExit() event methods.

You can see in the Console that the there’s no more log messages appearing, or any signs of the numbers of collapsed log messages increasing.

It’s been giving me a tough time to find a solution to this, but I don’t know how to make out of it. Does anyone know?

Solved.

I’m an idiot. I forgot to untick the “Use Gravity” in the Rigidbody…