Problems with colliders
Im trying to make the (floor1 change to the floor2) and the (floor2 change to the floor1) when the player object enter in their colliders. The problem is obviously that i am triggering the two triggers at the almost same time, and the floors keep changing unless i step out of them. I want to keep the floors triggering once per time, whithout using OnTriggerExit2D, must be OnTriggerEnter2D.
Im using the following code to the floors:
public class changeFloor_AtoB : MonoBehaviour {
public GameObject fa;
public GameObject fb;
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
fa.SetActive(false);
fb.SetActive(true);
}
}
}
public class changeFloor_BtoA : MonoBehaviour {
public GameObject fa;
public GameObject fb;
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
fa.SetActive(true);
fb.SetActive(false);
}
}
}
Answer by PizzaPie · May 07, 2017 at 11:52 AM
You should use an in independent collider-trigger to do so, remove the colliders-triggers from the other two objects and place a new EmptyObject with collider-trigger on the same spot. Then attach this script on it :
public class SwitchFloors : MonoBehaviour
{
public GameObject fa; //add reference on inspector
public GameObject fb; //add reference on inspector
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.CompareTag("Player"))
{
fa.SetActive(!fa.activeInHierarchy);
fb.SetActive(!fb.activeInHierarchy);
}
}
}
The reason your scripts doesn't work is that when you set on the other floor the OnTriggerEnter is called insantly so it changes the floor again and again. Cheers
Your answer
![](https://koobas.hobune.stream/wayback/20220612121451im_/https://answers.unity.com/themes/thub/images/avi.jpg)