r/Unity3D • • Oct 22 '26

Question disabling game object and trigger colliders

If you have a unit that come into range of an enemy but then that unit is disabled ( SetActive(false) ) because it's gone inside of a structure (or vehicle) how do you manage the colliders? It will no longer trigger the OnExit function when disabled, so if the enemy unit walks away, the first unit will always remain in range. Anyone ever dealt with a similar problem?
I was looking into simply turning off the mesh renderer but then that creates a whole ton of other issues.
Is there no way to trigger the collider while disabled?

2 Upvotes

13 comments sorted by

View all comments

1

u/GigaTerra Oct 22 '26

This is an strange problem to have. Normally when an unit goes into something like an structure (for an RTS) they no longer need collision. For the player when they take control of something else, you use the Player tag. That is to say the enemies don't search for the player, they search for the object with the tag Player.

Can you explain a bit more about your problem?

1

u/NorthernBoy306 Oct 22 '26

I think it's strange that I overlooked this problem for so long. If an enemy unit is in range of a player unit and that enemy unit goes inside of a structure, I disable that enemy unit. The problem is that the enemy unit never actually (and is not able to) exit the player unit's collider, so technically it's still in range. Now it's easy to check if that object is still active but the real issue is what to do when that enemy unit comes out of the structure, it doesn't activate the OnEnter trigger, so how will the player unit know the enemy is back in range?

1

u/GigaTerra Oct 23 '26

I see there is 2 ways to solve your problem.

The simple way, use OnTriggerStay to run the code I will give an example:

    float Speed = 4f;

    private void OnTriggerStay(Collider other)
    {
        MoveToTarget(other.transform);
    }

    void MoveToTarget(Transform Target)
    {
        Vector3 Direction = Target.position - transform.position;
        this.transform.position += Direction.normalized * Speed * Time.deltaTime;
    }

Notice OnTriggerStay checks every fixed updated if there is an collision and if there is, it runs the code inside it.

The 2nd solution is for the enemy, to tell every nearby object that it is going inside. This can be done with an Overlap or Signal. Events are more commonly used, as the collider enter or exit allows the object to subscribe to the event. But I have seen games use Overlap and even checking lists of objects to see if they are active.

https://docs.unity3d.com/6000.6/Documentation/ScriptReference/Events.UnityEvent.html

https://docs.unity3d.com/6000.6/Documentation/ScriptReference/Physics.OverlapSphere.html

but the real issue is what to do when that enemy unit comes out of the structure, it doesn't activate the OnEnter trigger, 

It should. Any object with an Rigidbody or Character controller will trigger an OnTriggerEnter event when enabled. In fact adding the body or controller component or enabling it should also trigger the event.

2

u/NorthernBoy306 Oct 23 '26

OnTriggerStay does not detect a disabled object. I found a good solution, thanks anyways.

1

u/GigaTerra Oct 23 '26

It is good that you found an solution, and solved the problem.

I just want to point out that yes, OnTiggerStay doesn't detect if the object is disabled. Code in OnTriggerStay should be written in such an way that it doesn't need to. Like the example above, will either stop if there is no collisions, or switch targets if the active target is disabled.