One of my game buttons is giving errors, even though second button with same code works fine?

Hi all,
I am making two buttons in my game, in which one button plays “Spin Attack” animation and other “Double Chop” animation. I have added two separate functions to each button but with exact same code (can see in the code sample below). When I press the Attack Button (AttackButton() function is added to it) in the Unity, it works fine.

But when I press the Spin Attack button (to which SpinButton() function is added to it), its throwing error :-

UnassignedReferenceException: The variable anim of PlayerControllerAndroid has not been assigned.
You probably need to assign the anim variable of the PlayerControllerAndroid script in the inspector.

I have assigned anim variable in the script as you can see in the code and Player Component has been added to it, then why is it throwing this error? Yes, “SpinAttack” and “DoubleChop” are genuine and correct Animation names here.
I am stuck on this issue for a while now, it seems basic problem but please do help!

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

public class PlayerControllerAndroid : MonoBehaviour
{
    
    [SerializeField] private float moveSpeed = 10.0f;
    [SerializeField] private LayerMask layerMask;
    [SerializeField] private FixedJoystick joystick;

    private CharacterController charachterController;
    private Vector3 currentLookTarget = Vector3.zero;
    private Animator anim;
    private BoxCollider[] swordColliders;
    private GameObject fireTrail;
    private ParticleSystem fireTrailParticles;
    // Start is called before the first frame update
    void Start()
    {
        charachterController = GetComponent <CharacterController> ();
        anim = GetComponent <Animator> ();
        swordColliders = GetComponentsInChildren <BoxCollider> ();
        fireTrail = GameObject.FindWithTag("Fire") as GameObject;
        fireTrail.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
        if(!GameManager.instance.GameOver)
        {
            Vector3 moveDirection = new Vector3(joystick.Horizontal,0,joystick.Vertical);
            charachterController.SimpleMove (moveDirection*moveSpeed);

            if(moveDirection == Vector3.zero)
            {
                anim.SetBool("IsWalking",false);
            }else{
                anim.SetBool("IsWalking",true);
            }     
        }
    }

    void FixedUpdate()
    {
        Vector3 joystickPosition = new Vector3(joystick.Horizontal,0,joystick.Vertical);  
           
        Quaternion rotation = Quaternion.LookRotation(joystickPosition);
        transform.rotation = Quaternion.Lerp(transform.rotation,rotation,Time.deltaTime*10f);       
    }


    public void BeginAttack()
    {
        foreach (var weapon in swordColliders)
        {
            weapon.enabled = true;
        }
    }
    
    public void EndAttack()
    {
        foreach (var weapon in swordColliders)
        {
            weapon.enabled = false;
        }
    }

    public void speedPowerUp()
    {
        StartCoroutine(fireTrailRoutine());
    }

    IEnumerator fireTrailRoutine()
    {
        fireTrail.SetActive(true);
        moveSpeed = 13f;
        yield return new WaitForSeconds(10f);
        moveSpeed = 6f;
        fireTrailParticles = fireTrail.GetComponent<ParticleSystem> ();
        var em = fireTrailParticles.emission;
        em.enabled = false;
        yield return new WaitForSeconds(3f);
        em.enabled = true;
        fireTrail.SetActive(false);
    }

    public void SpinButton()
    {
        if(!GameManager.instance.GameOver)
            anim.Play("SpinAttack");            
    }

    public void AttackButton()
    {
        if(!GameManager.instance.GameOver)
            anim.Play("DoubleChop");
    }
}

Greetings @roopesh23

Well, I’ve tried the same thing from scratch. Two animations (on the same game object) Grow, which scales, and Move, which changes position. Here’s my code:

using UnityEngine;

public class TestAnim : MonoBehaviour
{
    Animator anim;

    void Start()
    {
        anim = GetComponent<Animator>();
    }

    public void GrowButton()
    {
        anim.Play("Grow");
    }

    public void MoveButton()
    {
        anim.Play("Move");
    }
}

As you can see, it’s pretty minimalistic. I’ve linked the two buttons to their respective public methods and it works as expected.

In your case, I’d first check that you are definitely creating animations on the same game object. Here’s my Animation window:

200253-screenshot-2022-09-29-at-121823.png

Then I’d check that the two buttons are definitely calling the same game object (if you had an earlier version of the code kicking around, or are pointing at the wrong Game Object, that could be the problem). If all else fails, try reducing the code. Copy it and take out everything that you don’t need; see if you can get down to a minimal version like mine and try again. Let us know how you get on.