- Home /
How do I only let one collider be affected at a time when using OnTriggerEnter?
This sounds like a vague question, but hear me out. I am creating a game where you can drop items using Q and pick them up with E if you are inside its trigger collider. It is complicated, but in short, I use OnTriggerEnter to switch a bool from false to true, and OnTriggerExit to turn it to false. This works fine, but the moment my player is inside of two triggers at the same time and I try picking them up, my player picks up both at the same time and glitches out a lot of other things. I would like only one item to be picked up each time I press E. Is there a way to only use one trigger at a time in OnTriggerEnter? Thanks!
Answer by carl010010 · Feb 07, 2019 at 03:47 AM
@Mochna What I would do is onTriggerEnter add the object you want to pick up to a list of objects. and on TriggerExit remove the object from the list.
Then in your update method when the player Presses E add that object to your inventory
List<GameObjects> list = new List<GameObjects>();
void OnTriggerEnter()
{
//Get your object
//Add it to the list
list.Add(object);
}
void OnTriggerExit()
{
//Get your object
//Add it to the list
list.Remove(object);
}
void Update()
{
//When the player presses E
{
//Add the object ontop of the list to your inventory
inventory.Add(list[list.length-1]);
list.Remove(list[list.length-1]);
}
}