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());
}
}