How do I check if a function has been called recently?

Hi, I’m working on a script where the player takes 1 damage when being crushed in between two objects. However, the issue I’m having is that if the objects are traveling fast enough, the player takes 2 damage.

What I’m currently trying to do is check if a function has recently been called, and if so, to not call the function again. I also tried adding an invulnerability function, but I wasn’t able to get that to work. Here’s the code as it is-- still learning coding in general, so I apologize if there’s some bizarre practice going on. Thank you!

PlayerController script:

if (isCrushed)
        {
            RaycastHit2D hit = new RaycastHit2D();
            hit = Physics2D.Raycast(transform.position, Vector2.up, 5.0f, whatIsHexPlate);

            if (hit.collider != null && hit.collider.tag == "HexPlate")
            {
                hit.collider.gameObject.transform.GetComponent<Rigidbody2D>().AddForce(upHpm);
            }

            //Deduct 1 health.
            playerHealth.TakeDamage(1);
        }

PlayerHealth script:

public void TakeDamage(int amount)
    {
        damaged = true;
        currentHealth -= amount;

        if (currentHealth <= 0 && !isDead)
        {
            Death();
        }
    }

    public void Invulnerable(float time)
    {
        damaged = false;
        invulnerableTimeMax = time;

        if (Time.deltaTime < invulnerableTimeMax)
        {
            damaged = false;
            isInvulnerable = true;
        }
        else if (Time.deltaTime >= invulnerableTimeMax)
        {
            isInvulnerable = false;
        }
    }

hello FlyingMeta. Making your charakter invulnerable for a short time after hit can be done by various methods. One that I like to use for such things is to make a bool that flips to true and prevents further damaging before flipping back to false.

    public bool invulnerable = false;
    public float invulnerableTime = 2f;

	void Update () {
        if (!invulnerable)
        {
            if (isCrushed)
            {
                //yourCode
                //Deduct 1 health.
                //playerHealth.TakeDamage(1);
                invulnerable = true;
            }    
        }
        else
        {
            invulnerableTime -= Time.deltaTime;
            if (invulnerableTime <= 0)
            {
                invulnerableTime = 2f;
                invulnerable = false;
            }
        }    
    }

you can as well make a coroutine to count down the time and gets started as you are damaged.

	void Update () {
        if (!invulnerable)
        {
            if (isCrushed)
            {
                //yourCode
                //Deduct 1 health.
                //playerHealth.TakeDamage(1);
                invulnerable = true;
                StartCoroutine(InvulnerableCountdown(invulnerableTime));
            }
        }
    }

    IEnumerator InvulnerableCountdown(float time)
    {
        yield return new WaitForSeconds(time);
        invulnerable = false;
    }

hope it helps (: