Simultaneus conditions not working (Beginner)

Hello!

In the following code the first two conditions work properly but I can´t have the final message when both get to the finish line…

namespace MoreMountains.CorgiEngine
{
    public class TriggerFinishLine : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {      
         
        }
       
        public void OnTriggerEnter2D(Collider2D finish)
    {       bool metazero = false; // players are not at finish line
            bool metaone = false;
            if (finish.gameObject == MultiplayerLevelManager.Instance.Players[0].gameObject)

               {
                    Debug.Log("Nico get to the finish line!");
                metazero = true; // player 0 get to the finish line
              
            }
            if (finish.gameObject == MultiplayerLevelManager.Instance.Players[1].gameObject)

            {
                Debug.Log("Pablo get to the finish line!");
                metaone = true; // player 1 get to the finish line
            }
               
           
            if (metazero == true && metaone == true)// If both players get into the finish line
            {
                Debug.Log("BOTH get to the finish line");
               
            }
        }
}
}

Thank you in advance for your time!!

The collided object will only match one or the other in a given collision, but you’re making a new metazero and metaone bool each time (they’re local scope variables and cease to exist the instant you leave the collision function), so they always start as false before you check the collisions. If you move those outside the function, they can persist:

private bool metazero = false;
private bool metaone = false;

public void OnTriggerEnter2D(Collider2D finish) {
if (finish.gameObject .........
}
2 Likes

That solved the problem! Thank you very much @StarManta