How do I add animations for a turn based game in my script?

I’m making a turn based game using Brackey’s tutorial and everything works. However, I can’t seem to figure out how to add / trigger animations using boolean. I tried to call the animation in another script, but the animation is still set to idle.


BattleSystem Script (which include a code calling for the animation)

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

public enum BattleState { START, PLAYERTURN, ENEMYTURN, WON, LOST, GUARD }

public class BattleSystem : MonoBehaviour
{
    public GameObject playerPrefeb;
    public GameObject enemyPrefab;

    Unit playerUnit;
    Unit enemyUnit;

    public TMP_Text dialogueText;

    public BattleHud playerHud;
    public BattleHud enemyHud;

    public BattleState state;
    
    void Start()
    {
        state = BattleState.START;
        StartCoroutine(SetupBattle());
    }

    IEnumerator SetupBattle()
    {
        GameObject playerGO = Instantiate(playerPrefeb);
        playerUnit = playerGO.GetComponent<Unit>();

        GameObject enemyGO = Instantiate(enemyPrefab);
        enemyUnit = enemyGO.GetComponent<Unit>();

        dialogueText.text = "You have encountered an enemy SLIME!";

        playerHud.SetHUD(playerUnit);
        enemyHud.SetHUD(enemyUnit);

        yield return new WaitForSeconds(2f);

        state = BattleState.PLAYERTURN;
        PlayerTurn();
    }

    IEnumerator PlayerAttack()
    {
        playerUnit.Attack();

        bool isDead = enemyUnit.TakeDamage(playerUnit.damage);

        enemyHud.SetHP(enemyUnit.currentHP);
        dialogueText.text = " ";

        yield return new WaitForSeconds(2f);

        if (isDead)
        {
            state = BattleState.WON;
            EndBattle();
        }
        else
        {
            state = BattleState.ENEMYTURN;
            StartCoroutine(EnemyTurn());
        }
    }

Unit Script (which include code for the animation)

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

public class Unit : MonoBehaviour
{
    public int damage;
    public int stamina;

    public int maxHP;
    public int currentHP;

    public int maxStamina;
    public int currentStamina;

    public Animator anim;

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

    private void Update()
    {
        anim.SetBool("toIdle", true);
        anim.SetBool("toAttack", false);
    }

    public void Idle()
    {
        anim.SetBool("toIdle", true);
        anim.SetBool("toAttack", false);
    }

    public void Attack()
    {
            anim.SetBool("toIdle", false);
            anim.SetBool("toAttack", true);
        
    }

In the Unit script, you have an Update event. That’s setting the idle state every frame, which would explain why you are always idling. Are you sure you have the right name for that method? Perhaps it should be called something else?? Or perhaps you don’t need it at all, since you have an Idle method which does the same thing.

You don’t currently call PlayerUnit.Idle from anywhere and you do call PlayerUnit.Damage, which you don’t have a definition for anywhere. I guess you either didn’t give us the whole code or you’re still in the middle of building the game. In any case, let us know if removing the Update in Unit fixes the problem…