Checking triggers when they overlap

using UnityEngine;
using System.Collections;

public class Rendering : MonoBehaviour {

    public BoxCollider collider1, collider2, collider3;
    public object1, object2, object3;

    void Start() {
        object1 = GameObject.Find("Object1");
        object2 = GameObject.Find("Object2");
        object3 = GameObject.Find("Object3");
    }

    void OnTriggerStay(Collider col) {
        if (col != collider1) {
            object1.SetActive (false);
        } else {
            object1.SetActive (true);
        }
        if (col != collider2) {
            object2.SetActive (false);
        } else {
            object2.SetActive (true);
        }
        if (col != collider3) {
            object3.SetActive (false);
        } else {
            object3.SetActive (true);
        }
    }

}

What I am trying to accomplish is I want to hide certain objects if you are not in their collider. I am checking for the collision with the trigger in the code above, and it seems that when the triggers overlap, the system kind of breaks and it doesn’t hide what I want it to. Is there a way to check if the player is in more than one trigger or should I handle my if comparators differently? Let me know, thanks!

For one thing, this really only needs to be OnTriggerEnter and/or OnTriggerExit. Subsequent updates of OnTriggerStay will only continue to use the same values again and again.

One thing you can try is to keep a list of the colliders that your player is currently overlapping. OnEnter, add the collider to a list. OnExit, remove it from the list. Any time that list changes, call a function to update what is enabled or disabled based on the current state of that list. That way you can see if the player is overapping one or more colliders at any given time.

I was thinking about doing this, but I wanted to see if it was possible to do it with OnTriggerStay. I’ll use the other method. This answers my question, thanks!

1 Like