Why one the code doesn't work

The following code doesn’t work

[SerializeField] Animator m_Animator;
[SerializeField] GameObject m_WeaponOnHand, m_WeaponOnBack;
    
// Use this for initialization
 void Start () {
 }
    	
// Update is called once per frame
void Update () {
    if (Input.GetKeyDown(KeyCode.Q))
    {   
   AnimatorName.SetBool("ParameterName", !AnimatorName.GetBool("ParameterName"));
     }
}

But then I figure out that it does after I change it to:

[SerializeField] Animator m_Animator;
[SerializeField] GameObject m_WeaponOnHand, m_WeaponOnBack;
bool tmp;

// Use this for initialization
void Start () {
   tmp = m_Animator.GetBool("GetWeapon");
}
	
// Update is called once per frame
void Update () {
    if (Input.GetKeyDown(KeyCode.Q))
    {
        m_Animator.SetBool("GetWeapon", !tmp);
        tmp = !tmp;
     }
}

Why???

The following code doesn’t work

As in? Does it not compile? Is there a runtime error? Unexpected behaviour? More information is needed.

“AnimatorName” doesn’t seem to be defined anywhere, what is it supposed to be? I don’t imagine that you have an animator paramater called “ParameterName” either, that’s probably supposed to be something else. It looks like the top was simply an example that you are expected to edit, replacing AnimatorName with the name of your animator variable, and ParameterName with the name of your parameter.

I think if you took your top example, and simply replaced “AnimatorName” with “m_Animator”, and “ParameterName” with “GetWeapon”, it would work.

Erm… because AnimatorName is not a reference to your animator component, and “ParameterName” is not the name of one of its parameters.

Your second example works because it uses m_Animator and “GetWeapon” instead.