Trying to get my melee attack system to recognise a hit.

So I have a public static variable on my PlayerClass script that is set to false initially, then in the update method it is set to true if the player’s attacking animation is playing:

public static bool isAttacking=false;

void Update()
{
    if(combatAnimator.GetCurrentAnimatorStateInfo(0)=="AttackAnimationTest")
    {
        Debug.Log("Attacking!");
        isAttacking=true;
    }
}

This section of the code works (the “attacking!” text pops up in the console). However, I have another script, WeaponAttackScript, that says that if this “PlayerClass.isAttacking” variable is true, and if the weapon enters an enemy collider, then it registers a hit:

void OnCollisionEnter (Collision enemyCollision)
{
    if (enemyCollision.gameObject.tag == "Enemy")
    {
        if (PlayerClass.isAttacking == true)
        {
            Debug.Log ("Hit!")
        }
    }
}

However, it turns out that a hit is only registered on a collision if the player ISN’T attacking, so if I just run into the enemy, then a hit is registered, but if I attack the enemy, nothing happens. Where have I gone wrong?

Edit for clarification: the weapon script has been placed on a sword mesh, along with a rigidbody component and a mesh collider. The sword is parented to the player character. The enemy has a capsule collider and no rigidbody.

Does your script ever reset isAttacking back to false? If not, what you describe may be a side effect. Consider:

isAttacking = (combatAnimator.GetCurrentAnimatorStateInfo(0)=="AttackAnimationTest");
if (isAttacking) Debug.Log("Attacking!");