Something is missing. Four Errors, 2 commas and 2 semi colons missing...

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


}

Because there’s no such construct as “if () else () else ()” which if you look at your code, is that you’ve typed.

Are you sure the closing bracket on line 82 is meant to be there and not after the “else ()”.

1 Like

You are absolutely correct. It was supposed to be a second if/else statement inside the else block if unit chance is true… Replying on mobile but I’m betting when I fire up my PC that’ll fix it.

i stand corrected. cant put an if else statement inside of an else block but i changed it to if else if else and its that same error. hmmmm

Side note the “int playerUnit.damage=0;” on line 96 (the location of the first error) has a different coloured “int” declaration than all the previous ones. Not sure what it means but it makes it look like its apart of the variable as opposed to delcaring the type int. Ill keep at it

AHA!! So in my smaller unit script I had already declared unit damage to be an int, removed both “int” declarations and voila no more erorrs!! Thanks again sir, you definitely spotted an error that I would’ve had to deal with eventually!

That line has no meaning.

This:

int foo = 1;

Means “declare an integer, name it foo and set its value to 1”

This:

int playerUnit.damage = 0;

Means “declare an integer, name it playerUnit.damage and set its value to 0”

But… playerUnit.damage is not a valid name.

If you want that code to set the .damage field of your playerUnit property to zero, then remove the int type declaration.

playerUnit.damage = 0;

It sounds like you could perhaps benefit from watching a few basic Unity coding tutorials to get the hang of all the myriad things you already have going. You may be building complexity that is beyond your ability to reason about, and this almost never is a useful way to learn.