Variable sets to false when it is not supposed to.

Hello. In my game, a enemy has a gun and when the player enters the trigger area, it is supposed to shoot every couple of seconds (if the player is still in the area of course). However, it is only firing once if the player doesn’t leave the area. I’ve worked out that this is because, for some reason, after the co-routine has gone through once, the variable (inArea) goes back to false but I don’t know why this is happening. Can anyone help?
Here is the part of my code responsible for the shooting:

    void  OnTriggerEnter2D ( Collider2D other  ){
        if (other.gameObject.tag == "Player")

            inArea = true;
        StartCoroutine(Shoot());

    }

    void OnTriggerExit2D ( Collider2D other )
    {
        if (other.gameObject.tag == "Player")
            justComeOut = true;
            inArea = false;
            StartCoroutine(Wait());


    }

    IEnumerator Shoot()
    {

        Debug.Log("INAREA IS " + inArea + "JUSTCOMEOUT" + justComeOut);
        if (inArea == true && justComeOut == false)
        {
            Transform clone = Instantiate(laser, transform.position, transform.rotation) as Transform;
            yield return new WaitForSeconds(timeToWait);
            StartCoroutine(Shoot());
        }


    }



    IEnumerator Wait()
    {
            yield return new WaitForSeconds(3);
            justComeOut = false;
    }
void OnTriggerExit2D ( Collider2D other )
    {
        if (other.gameObject.tag == "Player")
            justComeOut = true;
            inArea = false;
            StartCoroutine(Wait());
    }

to

void OnTriggerExit2D ( Collider2D other )
    {
        if (other.gameObject.tag == "Player")
  {
            justComeOut = true;
            inArea = false;
            StartCoroutine(Wait());
  }
    }

the problem is that when your bullet leaves the inArea becomes false

Replace your Shoot method with:

IEnumerator Shoot()
    {
        while (inArea == true && justComeOut == false)
        {
            Transform clone = Instantiate(laser, transform.position, transform.rotation) as Transform;
            yield return new WaitForSeconds(timeToWait);
        }
    }

This is the gun script part, not the bullet, isn’t it?

It worked. Thanks, Aldo.
The only problem is, it doesn’t take into consideration the WaitForSeconds in the Shoot co-routine and is constantly shooting when the player is in the trigger area.

same thing

void  OnTriggerEnter2D ( Collider2D other  ){
        if (other.gameObject.tag == "Player"){
            inArea = true;
        StartCoroutine(Shoot());
}
    }

No, that part works fine. It’s just not waiting between shoots.

EDIT: Never mind, I forgot some brackets :S