How to correctly activate and deactivate object from script ?

Hi everyone,
I have a GameObject called XWeaponTrail that is attached to each sword that is attached to each hand. It activates a trail after the sword, but it’s constantly activated. I wanted to make it so that the trail only activates when the attack animation starts, but it’s giving me a bunch of erros and i don’t know what to do to solve them.
Here is the script and the errors:
This is the particular part that is giving me problems:

void Update()
    {
        // fly
        if (Input.GetButtonDown ("Fly"))
            fly = !fly;
        aim = Input.GetButton ("Aim");
        h = Input.GetAxis ("Horizontal");
        v = Input.GetAxis ("Vertical");
        run = Input.GetButton ("Run");
        sprint = Input.GetButton ("Sprint");
        isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;
        if (Input.GetMouseButtonDown (0)) {
            anim.SetBool ("Attack", true);
        } else {
            anim.SetBool ("Attack", false);
        }
           
        if (anim_Animator.GetCurrentAnimatorStateInfo(0).IsName("attack") {
            XWeaponTrail.SetActive(true);
        } else {
            XWeaponTrail.SetActive(false);
        }
    }

and this is the whole code:

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour
{



    public float walkSpeed = 0.15f;
    public float runSpeed = 1.0f;
    public float sprintSpeed = 2.0f;
    public float flySpeed = 4.0f;

    public float turnSmoothing = 3.0f;
    public float aimTurnSmoothing = 15.0f;
    public float speedDampTime = 0.1f;

    public float jumpHeight = 5.0f;
    public float jumpCooldown = 1.0f;

    private float timeToNextJump = 0;
   
    private float speed;

    private Vector3 lastDirection;

    private Animator anim;
    private int speedFloat;
    private int jumpBool;
    private int hFloat;
    private int vFloat;
    private int aimBool;
    private int flyBool;
    private int groundedBool;
    private Transform cameraTransform;

    private float h;
    private float v;

    private bool aim;

    private bool run;
    private bool Attack;
    private bool sprint;

    private bool isMoving;

    // fly
    private bool fly = false;
    private float distToGround;
    private float sprintFactor;

    static int attackState = Animator.StringToHash("Base.attack");

    public static PlayerControl Instance;

    GameObject XWeaponTrail;

    void Awake()
    {
        anim = GetComponent<Animator> ();
        cameraTransform = Camera.main.transform;

        speedFloat = Animator.StringToHash("Speed");
        jumpBool = Animator.StringToHash("Jump");
        hFloat = Animator.StringToHash("H");
        vFloat = Animator.StringToHash("V");
        aimBool = Animator.StringToHash("Aim");
        // fly
        flyBool = Animator.StringToHash ("Fly");
        groundedBool = Animator.StringToHash("Grounded");
        distToGround = GetComponent<Collider>().bounds.extents.y;
        sprintFactor = sprintSpeed / runSpeed;
    }


    bool IsGrounded() {
        return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
    }
    void Update()
    {
        // fly
        if (Input.GetButtonDown ("Fly"))
            fly = !fly;
        aim = Input.GetButton ("Aim");
        h = Input.GetAxis ("Horizontal");
        v = Input.GetAxis ("Vertical");
        run = Input.GetButton ("Run");
        sprint = Input.GetButton ("Sprint");
        isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;
        if (Input.GetMouseButtonDown (0)) {
            anim.SetBool ("Attack", true);
        } else {
            anim.SetBool ("Attack", false);
        }
           
        if (anim_Animator.GetCurrentAnimatorStateInfo(0).IsName("attack") {
            XWeaponTrail.SetActive(true);
        } else {
            XWeaponTrail.SetActive(false);
        }
    }

    void FixedUpdate()
    {
        anim.SetBool (aimBool, IsAiming());
        anim.SetFloat(hFloat, h);
        anim.SetFloat(vFloat, v);
       
        // Fly
        anim.SetBool (flyBool, fly);
        GetComponent<Rigidbody>().useGravity = !fly;
        anim.SetBool (groundedBool, IsGrounded ());
        if(fly)
            FlyManagement(h,v);

        else
        {
            MovementManagement (h, v, run, sprint);
            JumpManagement ();
        }
        { currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
        if (currentBaseState.nameHash == attackState){
                        XWeaponTrail.SetActive(true);
                    } else {
                        XWeaponTrail.SetActive(false);
                    }


                       
    }
   
    void Start(){
        anim = GetComponent<Animator>();
    }
    // fly
    void FlyManagement(float horizontal, float vertical)
    {
        Vector3 direction = Rotating(horizontal, vertical);
        GetComponent<Rigidbody>().AddForce(direction * flySpeed * 100 * (sprint?sprintFactor:1));
    }

    void JumpManagement()
    {
        if (GetComponent<Rigidbody>().velocity.y < 10) // already jumped
        {
            anim.SetBool (jumpBool, false);
            if(timeToNextJump > 0)
                timeToNextJump -= Time.deltaTime;
        }
        if (Input.GetButtonDown ("Jump"))
        {
            anim.SetBool(jumpBool, true);
            if(speed > 0 && timeToNextJump <= 0 && !aim)
            {
                GetComponent<Rigidbody>().velocity = new Vector3(0, jumpHeight, 0);
                timeToNextJump = jumpCooldown;
            }
        }
    }

    void MovementManagement(float horizontal, float vertical, bool running, bool sprinting)
    {
        Rotating(horizontal, vertical);

        if(isMoving)
        {
            if(sprinting)
            {
                speed = sprintSpeed;
            }
            else if (running)
            {
                speed = runSpeed;
            }
            else
            {
                speed = walkSpeed;
            }

            anim.SetFloat(speedFloat, speed, speedDampTime, Time.deltaTime);
        }
        else
        {
            speed = 0f;
            anim.SetFloat(speedFloat, 0f);
        }
        GetComponent<Rigidbody>().AddForce(Vector3.forward*speed);
    }

    Vector3 Rotating(float horizontal, float vertical)
    {
        Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
        if (!fly)
            forward.y = 0.0f;
        forward = forward.normalized;

        Vector3 right = new Vector3(forward.z, 0, -forward.x);

        Vector3 targetDirection;

        float finalTurnSmoothing;

        if(IsAiming())
        {
            targetDirection = forward;
            finalTurnSmoothing = aimTurnSmoothing;
        }
        else
        {
            targetDirection = forward * vertical + right * horizontal;
            finalTurnSmoothing = turnSmoothing;
        }

        if((isMoving && targetDirection != Vector3.zero) || IsAiming())
        {
            Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up);
            // fly
            if (fly)
                targetRotation *= Quaternion.Euler (90, 0, 0);

            Quaternion newRotation = Quaternion.Slerp(GetComponent<Rigidbody>().rotation, targetRotation, finalTurnSmoothing * Time.deltaTime);
            GetComponent<Rigidbody>().MoveRotation (newRotation);
            lastDirection = targetDirection;
        }
        //idle - fly or grounded
        if(!(Mathf.Abs(h) > 0.9 || Mathf.Abs(v) > 0.9))
        {
            Repositioning();
        }

        return targetDirection;
    }   

    private void Repositioning()
    {
        Vector3 repositioning = lastDirection;
        if(repositioning != Vector3.zero)
        {
            repositioning.y = 0;
            Quaternion targetRotation = Quaternion.LookRotation (repositioning, Vector3.up);
            Quaternion newRotation = Quaternion.Slerp(GetComponent<Rigidbody>().rotation, targetRotation, turnSmoothing * Time.deltaTime);
            GetComponent<Rigidbody>().MoveRotation (newRotation);
        }
    }

    public bool IsFlying()
    {
        return fly;
    }

    public bool IsAiming()
    {
        return aim && !fly;
    }

    public bool isSprinting()
    {
        return sprint && !aim && (isMoving);
    }
}

I’d suggest using animation events to signal the start and end of the weapon attack. https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html

Your script looks fine at first glance. If I knew which errors you were getting, maybe I could help you with that.

I can’t help you with “a bunch of erros”, though.

If i remember corectly, you can actually activate/desactivate gameObjects and in a animation, or you could turn down the opacity of the texture to 0 ( a bit inefficient but eh dosen’t matter unless ita a aaa game)

Hi,
Sorry for not replying, i’ve been caught up in my studies in the past 2 days, and didn’t have time to check my project. I also forgot to post the screenshots of the errors, and i don’t have my project here and now, but I’ll look for them and post them later on. I think it said for example unexpected } after the 2 “if” commands. It also said other errors but anyway i’ll post them later. Thank you all for your help, I appreciate it

This means you probably have 1 to many { or } (basically that you didn’t close brackets)

Or it could be that you forgot a ; at the end of a line.

Sorry I’ve been very busy this last week and I haven’t had the time to work on my project. But here are the errors I get from the script. I haven’t changed it or anything. So could anyone help ?