Overlapping objects detecting the player, and regulating active states based on his position

Hi, so I have an odd question. I have some objects that activate when the player is inside of them, and deactivate when the player leaves. I want to be able to overlap them and still only have one active. specifically, the one the player most recently entered to still be active. I have tried for the last 8 hours to put the individual objects IDs into a list to try and determine which one is the “current room”. I think (due to 8 hours of failing at simple math logic) I’m doing something hard, rather than smart. Anyone have any idea how I could make these objects know when to shut off even if the player is technically still inside them?

public class Room : MonoBehaviour
{
   private static List<Room> rooms = new List<Room>();
   private static Room currentRoom = null;

   void OnEnable() { rooms.Add(this); }
   void OnDisable() { rooms.Remove(this); }

   void OnTriggerEnter(Collider other)
   {
     Activate();
     currentRoom = this;

     // turn off old rooms:
     foreach(Room room in rooms)
     {
       if(room != this)
          room.Deactivate();
     }
   }

    void OnTriggerExit(Collider other)
    {
       // only turn off if we're the active room:
       if(currentRoom == this)
       {
           Deactivate();
           currentRoom = null;
       }
    }
 

   // whatever you want to happen:
   public void Activate() { }
   public void Deactivate() {}
}

Hrmm…Okay, Thanks! I’ll play around with it a bit and post again when I get it working. :slight_smile:

Okay, so newbie question, I have a very basic idea of what the code actually does, and I’m not exactly sure where I would put the boundaries. does “this” get the current object or is it just a placeholder?

this means the current object, so room script in this case. So anytime you enter a new room’s trigger, it turns off every other room except this one. Is that not what you want?

It is indeed what I want. I was simply trying to understand what process it takes to do what it does and how to use it, because I am still a neophyte C# creature, and a rather inexperienced coder in general.

[
So to confirm, It makes list Room, which can contain instances of this script.

When the script is enabled, it adds itself to the list, and removes itself when disabled.

When we enter a new room (OnTriggerEnter), which takes a collider (the collider of the new room), set the instance of Room to currentRoom and calls activate();

when we enter a new room, it checks the list Room to see if this instance of room is contained. if it is not inside of it calls deactivate.

If a Room script instance is inside list room, and it is not the current instance, call on whatever instance of room that is Deactivate().

When we exit a room, if this instance of room is set to be currentRoom, deactivate the room set currentRoom to null.
]

And all of this would be attached directly to whatever object has the boundaries. Okay, so currently my boundary objects consist of a parent, that checks to see if the player is inside of it, and a child, which the parent activates/ deactivates depending if the player is inside/ outside of the parent. The camera gets it’s boundary from the child.

So with this script I would not need the parent-child system, and I could attach it to the parent object, while getting the boundary information for my camera from whether or not the script-attached-GameObject is inside of list room, instead of whether its active or not?

I’m not sure why, but OnTriggerEnter and OnTriggerExit are not being called. The GameObject the script is attached to has a BoxCollider2D set to Trigger, and the player has a BoxCollider2D and a CircleCollider2D. Is there anything I’m missing to make this work?

I am a blockhead(as opposed to square). I didn’t see there was a 2D option for the OnTrigger methods.

Did you get it sorted? Sorry for my late response: the trigger should go around the entire room, not individual objects within, since I assume you want to turn the entire room off or on.

Yes and no. I got it working perfectly (mechanics-wise) but I opened a new thread due to an error I was getting.