[Solved] Boolean is true and flase every frame

Hi every one
I’m trying to disable my (aiming) joystick while the player enter a door trigger and change the fire button to door opening button using a boolean
the boolean is not valued and changes with a method in another script.

The problem is canAim (boolean) is showing both true and false every frame using debug.log(canAim)

Here is the code related to joystick and boolean

  private void Update()
    {
        Debug.Log(canAim);
        int i = 0;
        while (i < Input.touchCount)
        {
            if (Input.GetTouch(i).position.x > Screen.width / 2 && canAim)
            {
                myTouch = Input.GetTouch(i);
                myTouchPosition = myTouch.position;
                touchID = myTouch.fingerId;
                touchID2 = myTouch.fingerId;
                isAiming = true;
            }
i++
}

and this is the code in fire button script:

    public void doorActionEnter()
    {
        GetComponent<Image>().sprite = doorSprite;
        door = selectedDoor.GetComponent<door>();
        doordetected = true;
        joystick.canAim = false;


    }
    public void doorActionExit()
    {
        GetComponent<Image>().sprite = fireSprite;
        doordetected = false;
        joystick.canAim = true;

    }

and this is the code in player script:

  private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("door"))
        {
            firebtn.selectedDoor = collision.gameObject;
            firebtn.doorActionEnter();
        }
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.CompareTag("door"))
        {
            firebtn.doorActionExit();
        }
    }

in the DebugLog function, you wrote Debug.Log(canAim);
instead use:

Debug.Log(joystick.canAim);

(you still need to put the bool’s “address”)

  1. You should check tags that door has “door” tag or player can interact door’s collider in
    Project settings >> Physics2D

  2. Try if you are not interacting with multiple doors:

    private void OnTriggerExit2D(Collider2D collision)
    {
    if (collision.CompareTag(“door”))
    {
    firebtn.selectedDoor = null;
    firebtn.doorActionExit();
    }
    }

and debug selected door or use it instead of canAim

This is a complex UI so this might be a little bit confusing.
I tried to make a boolean in fire button script:

 public bool canAim2;
 private void Update()
{
        if (doordetected) canAim2 = false;
        else canAim2 = true;
}

and use this in joy stick script:

public firebtn firebtn;

 private void Start()
{
        firebtn = GameObject.Find("fireBtn").GetComponent<firebtn>();
}
 private void Update()
{
        canAim = firebtn.canAim2;
}

I tried to reference to fire button script in inspector window but it seemed to send me null reference so I tried to do it in script as you can see and now every thing is working fine.
But I’m still wondering why the first solution did not worked out