How to avoid repeating the same action when the player leaves the trigger object area.

Hi all! I’m very new to Unity and C# (I started studying and experimenting only about 10 days ago), so please bear with me. I’m trying to make a basic 2D platform level, just to get the hang of it. Here’s my problem in a nutshell:

What I’m trying to do: When the player enters the trigger object area, the player freezes for three seconds, an object gets activated over the trigger object, then the object disappears and the player can move again.

My problem: Everything works as described above, but when the player leaves the trigger object area (as defined by the trigger object’s box collider borders), then the triggered action repeats itself (i.e. the player freezes for three seconds and the object appears over the trigger object for the same duration).

What I want: I want the player to be able to leave the trigger without triggering the same action again. However, I don’t want to disable the trigger, as I want the action to be able to repeat itself, if/when the player re-enters the trigger.

Here’s my script. I know it sucks, but for now I’m just focused on making things work as intended. Afterwards I’ll figure out how to make it more efficient.

public class JohnCheers : MonoBehaviour
{
    public bool hasBeer;
    public GameObject player;
    public Animator animator_john;
    public Animator animator_player;
    public GameObject noBeer;

    void Start()
    {
        hasBeer = false;
        noBeer.SetActive(false);
    }

    private void OnTriggerEnter2D(Collider2D collider)
    {
        if (hasBeer)
        {
            player.GetComponent<PlayerMovement>().runSpeed = 0f;
            player.GetComponent<PlayerMovement>().jump = false;
            animator_john.SetBool("Cheers2", true);
            animator_player.SetBool("Cheers", true);
            StartCoroutine(ToGameOver());

            IEnumerator ToGameOver()
            {
                yield return new WaitForSeconds(4);
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
            }
        }
        else
        {
            player.GetComponent<PlayerMovement>().runSpeed = 0f;
            noBeer.SetActive(true);
            StartCoroutine(FreezePlayer());

            IEnumerator FreezePlayer()
            {
                yield return new WaitForSeconds(3);
                noBeer.SetActive(false);
                player.GetComponent<PlayerMovement>().runSpeed = 40f;

            }
        }

    }
}

Thank you!

Hi again,

First of all, I’d like to thank the good people that took some of their precious time to help me out - much appreciated :slight_smile:

I finally figured it out. I had followed Brackeys’ tutorial to set up my player’s movement, and the guy suggested that we use two colliders for the same object (player); a box one and a circle one. I noticed that the trigger was firing twice when exiting the trigger area. So that got me thinking that maybe the two colliders had to do something with it. So I disabled the bottom (circle) collider (of course I adjusted the remaining box collider so that it would fit the player sprite). And voila, it worked like a charm :slight_smile: Now the message is displayed whenever the player enters the trigger and nothing happens when he leaves (as it should).

So as it turned out, this wasn’t a script thing (well, at least not this script). So no one could have figured it out just by the script that I posted.

Thank you again @Tehemus and @icehex . Cheers!