Trigger Collinder doesnt work with Input.GetKey

Hello guys, I’m trying to make a game where u can cut down trees, but when I tested my script it didn’t work at all. This script is for cutting down trees. When I put TakeDmg and Destroytree in the private void OnTriggerEnter2D(Collider2D other) it works fine, but when I add if(Input.GetKey(KeyCode.E)), it stops working
I really don’t know that’s the problem.

    public int treeHp;
    public GameObject logPrefab;
    public Animator anim;


    private void OnTriggerEnter2D(Collider2D other){

        if(Input.GetKey(KeyCode.E)){

            TakeDmg();
            Destroytree();
        }
    }
    public void TakeDmg(){
//animatie si particule de la copac 
        anim.SetTrigger("TreeDamage");    
        treeHp = treeHp -1;
    }

    public void Destroytree(){

        if(treeHp <= 0){
            //spawn la loguri 

            Destroy(gameObject);
        }
    }

Thanks :smiley:

Your problem is that OnTriggerEnter2D() is called only during the first frame that you begin touching a trigger collider. So unless you are pressing the “E” key during the frame that you begin touching the trigger collider, nothing will happen.

What you are probably looking for is using OnTriggerStay2D(), which is called during every frame that you are touching a trigger collider.