I can't seem to figure out why this one portion of my script won't work!

Basically, the rest of the code works fine for what I am trying to do. The only issue I am having is when I try to do “StartCoroutine(EnemyTurn());” at line 161, the “EnemyTurn” portion of the script isn’t really cooperating. Been working at this part for a little over 3 hours, and I can’t figure out what I’m doing wrong.
Bear with me, I’m pretty new to this and I’m trying my best to learn how to do all this still.
Any help would be greatly appreciated!

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

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

public class battlesystem : MonoBehaviour
{

    public GameObject playerPrefab;
    public GameObject enemyPrefab;

    public Transform playerBattleStation;
    public Transform enemyBattleStation;

    unit playerUnit;
    unit enemyUnit;

    public Text dialogueText;

    public BattleHud playerHUD;
    public BattleHud enemyHUD;

    public BattleState state;

    public int rngChance;
  

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

    }

    IEnumerator SetupBattle()
    {
        GameObject playerGO = Instantiate(playerPrefab, playerBattleStation);
        playerUnit = playerGO.GetComponent<unit>();

        GameObject enemyGO = Instantiate(enemyPrefab, enemyBattleStation);
        enemyUnit = enemyGO.GetComponent<unit>();

        dialogueText.text = "A " + enemyUnit.unitName + " approaches...";

        playerHUD.SetHud(playerUnit);
        enemyHUD.SetHud(enemyUnit);

        yield return new WaitForSeconds(2f);

        state = BattleState.PLAYERTURN;
        PlayerTurn();
    }




        IEnumerator PlayerAttack()
        {
            rngChance = Random.Range(0, 101);
            dialogueText.text = "You attacked " + enemyUnit.unitName;
            yield return new WaitForSeconds(1f);
            if (rngChance <= 20)
                {
                //add noise/animation here
                dialogueText.text = "Your hit missed!";
                yield return new WaitForSeconds(1f);
        }
            else
                {
                //add noise/animation here
                dialogueText.text = "Your attack was successful!";
                yield return new WaitForSeconds(1f);
                enemyUnit.TakeDamage(playerUnit.damage);
                enemyHUD.SetHP(enemyUnit.currentHP);
                }

            yield return new WaitForSeconds(0f);
            bool isDead = enemyUnit.currentHP <= 0;

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




        IEnumerator EnemyTurn()
            {
                dialogueText.text = enemyUnit.unitName + " attacks!";

                yield return new WaitForSeconds(1f);

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

                playerHUD.SetHP(playerUnit.currentHP);

                yield return new WaitForSeconds(1f);

                if(isDead)
                {
                    state = BattleState.LOST;
                    EndBattle();
                }
                else
                {
                    state = BattleState.PLAYERTURN;
                    PlayerTurn();
                }
            }


        void EndBattle()
        {
            if(state == BattleState.WON)
            {
                dialogueText.text = "You Won!";
            }
            else
            {
                dialogueText.text = "You were defeated...";
            }   


        }


        yield return new WaitForSeconds(0f);


        }

        void PlayerTurn()
        {
            dialogueText.text = "Choose An Action";

        }


    IEnumerator PlayerHeal()
    {
       
        playerUnit.Heal(10);

        playerHUD.SetHP(playerUnit.currentHP);
        yield return new WaitForSeconds(0f);
        dialogueText.text = "You feel renewed!";

        yield return new WaitForSeconds(2f);
           
            state = BattleState.ENEMYTURN;
            StartCoroutine(EnemyTurn());
    }

    public void OnAttackButton()
    {
        if (state != BattleState.PLAYERTURN)
            return;

        StartCoroutine(PlayerAttack());

    }

    public void OnHealButton()
    {
        if (state != BattleState.PLAYERTURN)
            return;

        StartCoroutine(PlayerHeal());

    }

}

If you fix all your indentations, especially your { and }, you would see that your EnemyTurn method is actually inside your PlayerAttack method. This is also the same with your EndBattle method (it is inside your PlayerAttack method as well).
Fix those so they are not inside the PlayerAttack method and see if that helps.

Excellent! Welcome!

It can sometimes be useful to stare at code, but other times you have to actually see how it is running in your full scene / game context to understand how it is going wrong.

Here is some magic sauce to give you greater insight into what your code is actually doing.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

You must find a way to get the information you need in order to reason about what the problem is.