OnTriggerEnter2D not working, alternatives?

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?

Ohhh, that’s your problem. Triggers won’t get called in parent object scripts. You have to have the script catching the trigger as a component of the exact same GameObject where the Trigger Collider actually is.

So, what’s your next step? Probably the easiest solution is to move that script into each of the children, while making the flag a static so that it’s value is shared between them.

Oh! this fixed it! Thanks Pyrian for trying to help me anyway :slight_smile: