Problem when switching weapons

Hello everyone.

I’m making an FPS game and I recently added the function in the game that allows the player to switch between weapons. But I keep getting one weird problem that I can’t seem to solve… When I switch to the next weapon I disable the current weapon GameObject and I enable the new one, but the problem is that if I switch to the new weapon while the animation is currently playing (e.g. ‘Attack’ animation or ‘Ready’ animation) I get weird position / rotation problems… I thinks it’s a bug in unity, but I’m not sure… I included the gif file of the problem and I also provided the weapon switch script.
Can someone help me with this? Any help is really appreciated! :slight_smile: Thank you!

GIF File: Imgur: The magic of the Internet

Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using PauseSystem;
using GameFunction.Binoculars;

public class WeaponsSwitch : MonoBehaviour
{
    public int currentWeapon = 0;
    public int maxWeapons = 2;
    [SerializeField] private float switchingTime = 1.15f;

    [SerializeField]
    private string[] WeaponName;

    public string currentWeaponName;

    [SerializeField]
    private Image[] SI;

    [SerializeField]
    private Color SI_Hover;

    [SerializeField]
    private Color SI_Default;

    [SerializeField]
    private GameObject SI_Holder;

    public Animator theAni;
    public GameObject[] weaponsList;
    public GameObject[] weaponsList_Ani;

    public AudioClip[] ReadySound;
    public AudioSource ReadyAudio;

    public Image bulletsIcon;
    public Sprite[] Bullets_Icons;

    public AudioClip SwitchSound;
    public AudioSource SwitchAudio;

    public Text BulletsText_left;
    public Text BulletsText_current;

    public bool isSwitching;

    [SerializeField]
    private GameObject[] Weapon_list;

    public GameObject Weapon_inHand;

    private PauseSystem.PauseSystem pauseSystem;
    private GameFunction.Binoculars.useBinoculars binoculars;

    [SerializeField] private LayerMask weaponsMask;
    [SerializeField] private LayerMask nothingMask;

    private Camera weaponCam;

    void Start ()
    {
        pauseSystem = GameObject.Find ("GameManager").GetComponent<PauseSystem.PauseSystem> ();
        binoculars = GameObject.FindObjectOfType<GameFunction.Binoculars.useBinoculars> ();
        weaponCam = GameObject.Find ("WeaponsCamera").GetComponent<Camera> ();
        //currentWeapon = 0;
        SelectWeapon(currentWeapon);
        SI_Holder.SetActive (false);
    }

    void Update ()
    {
        if(!GameManager.GameManager.gameManager.cantUseWeapons && !Weapon_inHand.gameObject.GetComponent<Weapon>().isShooting && !isSwitching && !GameManager.GameManager.gameManager.inGameFunction && !weaponsList_Ani [currentWeapon].GetComponent<Weapon>().isReloading)
        {
            if(Input.GetAxis("Next Weapon") > 0)
            {
                if(currentWeapon + 1 <= maxWeapons)
                {
                    currentWeapon++;
                }
                else
                {
                    currentWeapon = 0;
                }
                SelectWeapon(currentWeapon);
            }
            else if (Input.GetAxis("Previous Weapon") < 0)
            {
                if(currentWeapon - 1 >= 0)
                {
                    currentWeapon--;
                }
                else
                {
                    currentWeapon = maxWeapons;
                }
                SelectWeapon(currentWeapon);
            }

            if(currentWeapon == maxWeapons + 1)
            {
                currentWeapon = 0;
            }
            if(currentWeapon == -1)
            {
                currentWeapon = maxWeapons;
            }

            if(Input.GetKeyDown(KeyCode.Alpha1) && currentWeapon != 0)
            {
                currentWeapon = 0;
                SelectWeapon(currentWeapon);
            }
            if(Input.GetKeyDown(KeyCode.Alpha2) && currentWeapon != 1)
            {
                currentWeapon = 1;
                SelectWeapon(currentWeapon);
            }
            if(Input.GetKeyDown(KeyCode.Alpha3) && currentWeapon != 2)
            {
                currentWeapon = 2;
                SelectWeapon(currentWeapon);
            }
            if(Input.GetKeyDown(KeyCode.Alpha4) && currentWeapon != 3)
            {
                currentWeapon = 3;
                SelectWeapon(currentWeapon);
            }
        }
    }

    public void HideWeapon ()
    {
        weaponCam.cullingMask = nothingMask;
    }

    public void ShowWeapon ()
    {
        weaponCam.cullingMask = weaponsMask;
    }

    public void SelectWeapon (int index)
    {

        if (Weapon_inHand != null)
        {
            Weapon_inHand.GetComponent<Animator> ().Rebind ();
            StopCoroutine(Weapon_inHand.GetComponent<Weapon> ().waitTillAble ());
            Weapon_inHand.GetComponent<Weapon> ().canSwing = true;
        }
        Weapon_inHand = Weapon_list [index];
        if (Weapon_inHand.gameObject.GetComponent<Weapon> ().isShooting) return;

        SI_Holder.SetActive (true);
        StopAllCoroutines ();
        StartCoroutine (switching ());
        DisableAll ();

        weaponsList [index].SetActive (true);
        currentWeaponName = WeaponName [index];

        SI [index].color = SI_Hover;

        bulletsIcon.sprite = Bullets_Icons [index];

        ReadyAudio.clip = ReadySound [index];
        ReadyAudio.Play ();
        SwitchAudio.clip = SwitchSound;
        SwitchAudio.Play ();

        theAni = weaponsList_Ani [index].GetComponent<Animator> ();
        weaponsList_Ani [index].GetComponent<Weapon> ().isReloading = false;

        theAni.CrossFadeInFixedTime ("Ready", 0.01f);

        if(currentWeaponName != "Melee")
        {
            BulletsText_left.text = weaponsList_Ani [index].GetComponent<Weapon> ().bulletsLeft.ToString();
            BulletsText_current.text = weaponsList_Ani [index].GetComponent<Weapon> ().currentBullets.ToString();
        }
        else if(currentWeaponName == "Melee")
        {
            BulletsText_left.text = "";
            BulletsText_current.text = "";
        }

        //    CHECK        CURRENT AMMO DATA        AND DISPLAY TEXT EFFECTS
        textEffects();

        modeDisplay ();
    }

    private void textEffects ()
    {
        Weapon_inHand.gameObject.GetComponent<Weapon> ().textEffects ();
    }

    public void DisableAll()
    {
        foreach (GameObject weapon in weaponsList)
        {
            weapon.SetActive (false);
        }

        foreach (Image SelectIcons in SI)
        {
            SelectIcons.color = SI_Default;
        }
    }

    void modeDisplay ()
    {
        Weapon_inHand.gameObject.GetComponent<Weapon> ().displayModeInfo ();
    }
       
    IEnumerator switching ()
    {
        //isSwitching = true;
        yield return new WaitForSeconds (switchingTime);
        //isSwitching = false;

        SI_Holder.SetActive (false);
    }
}

Nevermind I just solved the problem!
The solution that worked is to rebind the weapon animator before switching to the new one.
(I updated the script)