I have been staring at this for far too long. I struggled to get the Chance method working for a turn-based battle system and had about 25 errors. Went through everything over the last few hours and just as I thought I was done these four errors popped up:
-
Assets\BattleSystem.cs(94,17): error CS1003: Syntax error, ‘,’ expected
-
Assets\BattleSystem.cs(94,18): error CS1002: ; expected
-
Assets\BattleSystem.cs(141,16): error CS1003: Syntax error, ‘,’ expected
-
Assets\BattleSystem.cs(141,17): error CS1002: ; expected
I feel really stupid because its two errors beside each other in two different spots(line 94 and 141). I have a unit script as well I’ll post at the end as well but its much smaller and I’m pretty sure I fixed the error that I did have… Anyways my code is a bit lengthy (234 lines is a lot to me lmao) but I keep seeing people ask for entire scripts so here it is.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Random=System.Random;
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;
// Start is called before the first frame update
void Start()
{
state = BattleState.START;
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 wild " + enemyUnit.unitName + " approaches...";
playerHUD.SetHUD(playerUnit);
enemyHUD.SetHUD(enemyUnit);
yield return new WaitForSeconds(2f);
state = BattleState.PLAYERTURN;
PlayerTurn();
}
//This is what i was working on
public int Chance()
{
var Random = new Random();
int hitChance = 0;
bool unitChance = playerUnit.agility >= enemyUnit.agility;
int agilityModifier = Math.Abs(playerUnit.agility - enemyUnit.agility);
//less
if (!unitChance)
{
//0-50%- (bad chance-possible miss)
int chance = Random.Next(0,51);
//enemy with higher agility lowers chance to hit
hitChance = chance - (agilityModifier * 2);
}
//greater or equal
else
{
//greater
if (agilityModifier > 0)
{
//60-120%-good Chance-possible critical hit
int chance = Random.Next(36, 120);
hitChance = chance + (agilityModifier * 5);
}
}
//equal
else
{
//36-100-normal Chance
int chance = Random.Next(36, 100);
hitChance = chance;
}
return hitChance;
}
IEnumerator PlayerAttack()
{
var Random = new Random();
int chance = Chance();
int playerUnit.damage = 0;
if (chance > 35)
{
playerUnit.damage = (int)Math.Round((playerUnit.strength * 1.25) - (enemyUnit.defence * 1.125));
dialogueText.text = playerUnit.unitName + "'s attack did " + playerUnit.damage + " damage!";
if (chance > 100)
{
//Critical hit
double multiplier = Random.NextDouble() + 0.25;
double critical = Math.Round(multiplier, 2);
playerUnit.damage = (int)Math.Round((playerUnit.strength * critical) - (enemyUnit.defence * 1.125));
dialogueText.text = "Critical hit!! " + playerUnit.unitName + "'s attack did " + playerUnit.damage + " damage!";
}
}
else
{
dialogueText.text = playerUnit.unitName + "'s " + " attack missed!";
}
bool isDead = enemyUnit.TakeDamage(playerUnit.damage);
enemyHUD.SetHP(enemyUnit.currentHP);
yield return new WaitForSeconds(2f);
if(isDead)
{
state = BattleState.WON;
EndBattle();
} else
{
state = BattleState.ENEMYTURN;
StartCoroutine(EnemyTurn());
}
}
IEnumerator EnemyTurn()
{
var Random = new Random();
int enemyUnit.damage = 0;
int chance = Chance();
dialogueText.text = enemyUnit.unitName + " attacks!";
yield return new WaitForSeconds(1f);
if (chance > 35)
{
enemyUnit.damage = Math.Round((enemyUnit.strength * 1.25) - (playerUnit.defence * 1.125));
dialogueText.text = enemyUnit.unitName + "'s attack did " + enemyUnit.damage + " damage!";
if (chance < 100)
{
//Critical hit
double multiplier = Random.NextDouble() + 0.25;
double critical = Math.Round(multiplier, 2);
enemyUnit.damage = (int)Math.Round((enemyUnit.strength * critical) - (playerUnit.defence * 1.125));
dialogueText.text = "Critical hit!! " + enemyUnit.unitName + "'s attack did " + playerUnit.damage + " damage!";
}
}
else
{
dialogueText.text = enemyUnit.unitName + "'s " + " attack missed!";
}
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 the battle!";
} else if (state == BattleState.LOST)
{
dialogueText.text = "You were defeated.";
}
}
void PlayerTurn()
{
dialogueText.text = "Choose an action:";
}
IEnumerator PlayerHeal()
{
playerUnit.Heal(5);
playerHUD.SetHP(playerUnit.currentHP);
dialogueText.text = "You feel renewed strength!";
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());
}
}
small unit script is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Unit : MonoBehaviour
{
public string unitName;
public int unitLevel;
public int strength;
public int defence;
public int agility;
public int maxHP;
public int currentHP;
public int damage;
public bool TakeDamage(int dmg)
{
currentHP -= dmg;
if (currentHP <= 0)
{
return true;
}
else
{
return false;
}
}
public void Heal(int amount)
{
currentHP += amount;
if (currentHP > maxHP)
{
currentHP = maxHP;
}
}
}