I’ve read that OnTriggerEnter2D has some issues, so I was wondering if there was another way to achieve what I need: getting the level to end after the 2 players are on their respective flag (there is no way one can get to the other’s flag), here’s what I had:
private int flag = 0;
void OnTriggerEnter2D(Collider2D col){
if (col.gameObject.tag == "Player") {
flag = flag + 1;
Debug.Log ("Flagged = " + flag);
}
}
void OnTriggerExit2D(Collider2D col){
if (col.gameObject.tag == "Player") {
flag -= 1;
Debug.Log ("Unflaged" + flag);
}
}
// Update is called once per frame
void Update () {
if (flag == 2) {
Debug.Log ("Level finished");
Application.LoadLevel ("1");
}
}
Each player is on a different layer but on the same tag (Player) because it’s impossible for them to get to the other’s space. The console prints
onTriggerEnterEvent: Goals
UnityEngine.Debug:Log(Object)
but it won’t add anything to the variable flag.
Is there other way to do this? or is this the correct way but I’m doing something wrong?