- Home /
Problem with multiple colliders in a trigger
I'm making a game where a character can pick up rocks and throw them at wood and then pick them up again.
The issue I'm having comes when trying to pick up a rock that lies right in front of the wood. The thing is that both the rock's and the wood's colliders are triggered when the players OnTriggerStay2D is run. The player only tries to pick up the wood and ignores the rock completely, even though they are both inside the trigger for the player.
The smaller rectangle right in front of the player is his trigger to pick things up, the brown block is the wood with a box collider and the round grey thing is a rock with a polygon collider. This code is attached to the player:
void OnTriggerStay2D(Collider2D col) {
if(Input.GetButton ("Pickup") && !holdPickup && !charInventory.isHoldingItem()) {
Debug.Log (col.gameObject);
holdPickup = true;
switch(col.gameObject.tag) {
case "rock" :
charInventory.setHoldingItem(col.gameObject);
col.gameObject.GetComponent<PickUpableItem>().PickedUp (this.gameObject);
break;
}
}
}
}
The debug-log only prints out "wood", which means that the method is only triggered on the wood- collider and ignores the stone. I have paused in-game and checked the range, but they are both in range for the players trigger.
Why is that? Why won't the method sometimes check for the rock?
Thanks!
Answer by jalal_moghaddam · Apr 06, 2017 at 03:32 PM
hi, i had same problem and solve it by making a trigger list
void OnTriggerEnter2D (Collider2D objectInTrigger)
{
triggerList.Add(objectInTrigger.name);
}
void OnTriggerExit2D(Collider2D objectExitTrigger)
{
triggerList.Remove (objectExitTrigger.name);
}
public void DoAction()
{
foreach (string triggerObjectName in triggerList)
{
switch (triggerObjectName) {
case "LightKey":
lightKeyEventScript.DoAction ();
break;
case "Toilet":
toiletEventScript.DoAction ();
break;
}
}
}
Answer by shadowpuppet · Apr 05, 2017 at 07:32 PM
give the rock a tag "rock" and add another condition
void OnTriggerStay2D(Collider2D other) {
if(other.tag == "rock")
{
if(Input.GetButton ("Pickup") && !holdPickup && !charInventory.isHoldingItem()) {
Your answer
![](https://koobas.hobune.stream/wayback/20220612084427im_/https://answers.unity.com/themes/thub/images/avi.jpg)