Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to check if game object is in a trigger area

Discussion in '2D' started by randellmcglynn, Jul 5, 2022.

  1. randellmcglynn

    randellmcglynn

    Joined:
    Jul 5, 2022
    Posts:
    2
    I have a 2d tilemap collider combined with a 2d composite collider to and set to be a trigger. What I want to do is check if there are any game objects within that trigger area. The catch however is that none of these game objects have colliders. I cannot use collider bounds either since the trigger area can be wrapped around game objects without them being in it. At this point I have a list of game objects (got them by tag name), and I'm looping through them to check if they are in the room.

    Code (CSharp):
    1.     public void ShowMapIconsInExposedRoom(Collider2D roomCollider)
    2.     {
    3.         foreach(GameObject go in MapIconGameObjects)
    4.         {
    5.             if (/*game object is in the room collider*/)
    6.             {
    7.                 go.SetActive(true);              
    8.             }
    9.         }
    10.     }


    Any help is appreciated.
     
  2. LethalInjection

    LethalInjection

    Joined:
    Jan 25, 2015
    Posts:
    36
    Why would a gameObject be in the "trigger area" not set off the trigger event ? are you saying the white space is an exception to the green area ?
     
  3. randellmcglynn

    randellmcglynn

    Joined:
    Jul 5, 2022
    Posts:
    2
    They never enter the trigger area, they are there when the scene loads. I suppose I should have explained it in the context of the game. So the scene loads, and all the game objects are loaded with the scene (chests, stairs, points of interest, etc...). As the player walks around and enters sections of the map (entering the trigger), that calls the method to find all the gameobjects of a certain tag name and check if they are in the trigger area so they can be shown on the map when the player enters that area.
     
  4. LethalInjection

    LethalInjection

    Joined:
    Jan 25, 2015
    Posts:
    36
    i think the proper solution is a collider around the player and collider around all the objects and let unity do the work for you.