An error occurred while connecting the weapon and player movement script, preventing the attack from being performed

I am creating an attack using these two scripts, but the Attack() method does not work.

The parts originally added to use the Weapon script in PlayerMovement were: Weapon weapon;, fireready, firedelay, wea(), attack() added this part

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

public class Weapon : MonoBehaviour
{
    public enum Type { Melee, Range }
    public Type type;
    public int damage;
    public float rate;
    public BoxCollider meleeArea;
    public TrailRenderer trailEffect;

    public void Use()
    {
        if(type == Type.Melee)
        {
            StopCoroutine("Swing"); 
            StartCoroutine("Swing");
        }
    }
    IEnumerator Swing()
    {
        // 1
        yield return new WaitForSeconds(0.1f); 
        meleeArea.enabled = true;
        trailEffect.enabled = true;

        //2
        yield return new WaitForSeconds(0.3f);
        meleeArea.enabled = false;

        //3
        yield return new WaitForSeconds(0.3f);
        meleeArea.enabled = false;
    }
}

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

public class PlayerMovement : MonoBehaviour
{
    Animator anim;
    public float walkSpeed = 10.0f;
    [SerializeField]
    private float runSpeed;
    private float applySpeed;
    public float rotateSpeed = 10.0f;
    public float jumpForce = 10.0f;          
    private bool isGround = true;           
    private bool isRun = false;
    private bool isFireReady = true;
    //public GameObject[] weapons;
    Weapon weapon;
    Rigidbody rb;                           
    float h, v;
    float fireDelay;

    void Start()
    {
        rb = GetComponent<Rigidbody>();   
        applySpeed = walkSpeed;
        anim = GetComponent<Animator>();
        weapon = GetComponent<Weapon>();
    }

    void Update()
    {
        tryrun();
        Jump();
        wea();
        Attack();
        Move();
    }

    void tryrun()
    {
        if (Input.GetKeyDown(KeyCode.LeftShift) && isGround)
        {
            Running();
            anim.SetBool("isRun", true);
            Debug.Log("Run");
        }
        else if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            RunningCancel();
            Debug.Log("Run Over");
            anim.SetBool("isRun", false);
        }
    }

    private void Running()
    {
        isRun = true;
        applySpeed = runSpeed;
    }
    private void RunningCancel()
    {
        isRun = false;
        applySpeed = walkSpeed;
    }
   
    void Move()
    {
        h = Input.GetAxis("Horizontal");
        v = Input.GetAxis("Vertical");

        Vector3 dir = new Vector3(h, 0, v).normalized;

        if (!(h == 0 && v == 0))
        {
            transform.position += dir * applySpeed * Time.deltaTime;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(dir), Time.deltaTime * rotateSpeed);
            anim.SetBool("isWalk", true);
        }
        else
        {
            anim.SetBool("isWalk", false);
        }
    }

    void Jump()
    {
        if (Input.GetKey(KeyCode.Space) && isGround)
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            anim.SetBool("isJump", true);
            isGround = false;          
        }
        else
        {
            anim.SetBool("isJump", false);
        }
    }

    void wea()
    {
        if(weapon != null)
        {
            weapon.gameObject.SetActive(!weapon.gameObject.activeSelf);
        }
    }

    void Attack()
    {
        if (weapon == null)
        {
            return;
        }
        fireDelay += Time.deltaTime;
        isFireReady = weapon.rate < fireDelay;

        if (Input.GetMouseButtonDown(0) && isFireReady)
        {
            weapon.Use();
            anim.SetTrigger("isAttack");
            fireDelay = 0;
            Debug.Log("Click");
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGround = true;
        }
    }

}

Don’t that method constantly being executed at update

void wea()
{
    if(weapon != null)
    {
        weapon.gameObject.SetActive(!weapon.gameObject.activeSelf);
    }
}

Mean that you are constantly toggling weapon gameobject on and off?

So try to replace

void wea()
{
    if(weapon != null)
    {
        weapon.gameObject.SetActive(!weapon.gameObject.activeSelf);
    }
}

with

void wea()
{
    if (weapon != null)
    {
        if (!weapon.gameObject.activeSelf)
        {
            weapon.gameObject.SetActive(true);
        }
    }
}

Because yeah you don’t want to toggle the weapon GameObject every frame, only if it’s inactive
That might be enough to fix everything