i have problem with player attacks enemy

i made player attacks enemy with box collider and now I want to make the box collider appear when player starts attacking and box collider to disappear when he stops attacking is there some way to fix this or should I make it in different way

Here is script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HurtEnemy : MonoBehaviour
{
public int damageToGive = 2;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
    
}

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Enemy")
    {
       EnemyHealthManager eHealthMan;
       eHealthMan = other.gameObject.GetComponent<EnemyHealthManager>();
       eHealthMan.HurtEnemy(damageToGive);
    }
}

}

Yeah, real simple. In your update, say something like:


if (Input.GetButton(“Fire1”)) { gameObject.GetComponent().enabled = true; } else { gameObject.GetComponent().enabled = false; }


But it would probably be easier to replace your OnTriggerEnter2D function with something like:


if (other.tag == “Enemy” && Input.GetButton(“Fire1”)) { EnemyHealthManager eHealthMan; eHealthMan = other.gameObject.GetComponent(); eHealthMan.HurtEnemy(damageToGive); }