Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by justicebrinston, Oct 14, 2022.

  1. justicebrinston

    justicebrinston

    Joined:
    Oct 14, 2022
    Posts:
    3
    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.


    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using Random=System.Random;
    7.  
    8. public enum BattleState { START, PLAYERTURN, ENEMYTURN, WON, LOST }
    9.  
    10. public class BattleSystem : MonoBehaviour
    11. {
    12.  
    13.     public GameObject playerPrefab;
    14.     public GameObject enemyPrefab;
    15.  
    16.     public Transform playerBattleStation;
    17.     public Transform enemyBattleStation;
    18.  
    19.     Unit playerUnit;
    20.     Unit enemyUnit;
    21.  
    22.     public Text dialogueText;
    23.  
    24.     public BattleHUD playerHUD;
    25.     public BattleHUD enemyHUD;
    26.  
    27.     public BattleState state;
    28.  
    29.     // Start is called before the first frame update
    30.     void Start()
    31.     {
    32.         state = BattleState.START;
    33.         StartCoroutine(SetupBattle());
    34.     }
    35.  
    36.     IEnumerator SetupBattle()
    37.     {
    38.         GameObject playerGO = Instantiate(playerPrefab, playerBattleStation);
    39.         playerUnit = playerGO.GetComponent<Unit>();
    40.  
    41.         GameObject enemyGO = Instantiate(enemyPrefab, enemyBattleStation);
    42.         enemyUnit = enemyGO.GetComponent<Unit>();
    43.  
    44.         dialogueText.text = "A wild " + enemyUnit.unitName + " approaches...";
    45.  
    46.         playerHUD.SetHUD(playerUnit);
    47.         enemyHUD.SetHUD(enemyUnit);
    48.  
    49.         yield return new WaitForSeconds(2f);
    50.  
    51.         state = BattleState.PLAYERTURN;
    52.         PlayerTurn();
    53.     }
    54.  
    55. //This is what i was working on
    56.     public int Chance()
    57.     {
    58.         var    Random = new Random();
    59.         int hitChance = 0;
    60.         bool unitChance = playerUnit.agility >= enemyUnit.agility;
    61.         int agilityModifier = Math.Abs(playerUnit.agility - enemyUnit.agility);
    62.         //less
    63.         if (!unitChance)
    64.         {
    65.             //0-50%- (bad chance-possible miss)
    66.             int chance = Random.Next(0,51);
    67.             //enemy with higher agility lowers chance to hit
    68.             hitChance = chance - (agilityModifier * 2);
    69.            
    70.         }
    71.        
    72.         //greater or equal
    73.         else
    74.         {  
    75.             //greater
    76.             if (agilityModifier > 0)
    77.             {
    78.                 //60-120%-good Chance-possible critical hit
    79.                 int chance = Random.Next(36, 120);
    80.                 hitChance = chance + (agilityModifier * 5);
    81.             }
    82.         }  
    83.             //equal
    84.             else
    85.             {
    86.                 //36-100-normal Chance
    87.                 int chance = Random.Next(36, 100);
    88.                 hitChance = chance;
    89.                
    90.             }
    91.  
    92.         return hitChance;
    93.     }  
    94.  
    95.     IEnumerator PlayerAttack()
    96.     {  
    97.         var Random = new Random();
    98.         int chance = Chance();
    99.         int playerUnit.damage = 0;
    100.         if (chance > 35)
    101.         {  
    102.             playerUnit.damage = (int)Math.Round((playerUnit.strength * 1.25) - (enemyUnit.defence * 1.125));
    103.             dialogueText.text = playerUnit.unitName + "'s attack did " + playerUnit.damage + " damage!";
    104.             if (chance > 100)
    105.             {
    106.  
    107.                 //Critical hit
    108.                 double multiplier = Random.NextDouble() + 0.25;
    109.  
    110.                 double critical = Math.Round(multiplier, 2);
    111.  
    112.  
    113.                 playerUnit.damage = (int)Math.Round((playerUnit.strength * critical) - (enemyUnit.defence * 1.125));
    114.                 dialogueText.text = "Critical hit!! " + playerUnit.unitName + "'s attack did " + playerUnit.damage + " damage!";
    115.                
    116.             }
    117.         }
    118.  
    119.         else
    120.         {
    121.            
    122.             dialogueText.text = playerUnit.unitName + "'s " + " attack missed!";
    123.         }
    124.        
    125.         bool isDead = enemyUnit.TakeDamage(playerUnit.damage);
    126.  
    127.         enemyHUD.SetHP(enemyUnit.currentHP);
    128.        
    129.  
    130.         yield return new WaitForSeconds(2f);
    131.  
    132.         if(isDead)
    133.         {
    134.             state = BattleState.WON;
    135.             EndBattle();
    136.         } else
    137.         {
    138.             state = BattleState.ENEMYTURN;
    139.             StartCoroutine(EnemyTurn());
    140.         }
    141.     }
    142.  
    143.     IEnumerator EnemyTurn()
    144.     {  
    145.         var Random = new Random();
    146.         int enemyUnit.damage = 0;
    147.         int chance = Chance();
    148.         dialogueText.text = enemyUnit.unitName + " attacks!";
    149.  
    150.         yield return new WaitForSeconds(1f);
    151.  
    152.        
    153.         if (chance > 35)
    154.         {  
    155.             enemyUnit.damage = Math.Round((enemyUnit.strength * 1.25) - (playerUnit.defence * 1.125));
    156.             dialogueText.text = enemyUnit.unitName + "'s attack did " + enemyUnit.damage + " damage!";
    157.             if (chance < 100)
    158.             {
    159.  
    160.                 //Critical hit
    161.                 double multiplier = Random.NextDouble() + 0.25;
    162.                 double critical = Math.Round(multiplier, 2);
    163.                 enemyUnit.damage = (int)Math.Round((enemyUnit.strength * critical) - (playerUnit.defence * 1.125));
    164.        
    165.                 dialogueText.text = "Critical hit!! " + enemyUnit.unitName + "'s attack did " + playerUnit.damage + " damage!";
    166.             }
    167.         }
    168.  
    169.         else
    170.         {
    171.            
    172.             dialogueText.text = enemyUnit.unitName + "'s " + " attack missed!";
    173.         }
    174.  
    175.         bool isDead = playerUnit.TakeDamage(enemyUnit.damage);
    176.  
    177.         playerHUD.SetHP(playerUnit.currentHP);
    178.  
    179.         yield return new WaitForSeconds(1f);
    180.  
    181.         if(isDead)
    182.         {
    183.             state = BattleState.LOST;
    184.             EndBattle();
    185.         } else
    186.         {
    187.             state = BattleState.PLAYERTURN;
    188.             PlayerTurn();
    189.         }
    190.  
    191.     }
    192.  
    193.     void EndBattle()
    194.     {
    195.         if(state == BattleState.WON)
    196.         {
    197.             dialogueText.text = "You won the battle!";
    198.         } else if (state == BattleState.LOST)
    199.         {
    200.             dialogueText.text = "You were defeated.";
    201.         }
    202.     }
    203.  
    204.     void PlayerTurn()
    205.     {
    206.         dialogueText.text = "Choose an action:";
    207.     }
    208.  
    209.     IEnumerator PlayerHeal()
    210.     {
    211.         playerUnit.Heal(5);
    212.  
    213.         playerHUD.SetHP(playerUnit.currentHP);
    214.         dialogueText.text = "You feel renewed strength!";
    215.  
    216.         yield return new WaitForSeconds(2f);
    217.  
    218.         state = BattleState.ENEMYTURN;
    219.         StartCoroutine(EnemyTurn());
    220.     }
    221.  
    222.     public void OnAttackButton()
    223.     {
    224.         if (state != BattleState.PLAYERTURN)
    225.             return;
    226.  
    227.         StartCoroutine(PlayerAttack());
    228.     }
    229.  
    230.     public void OnHealButton()
    231.     {
    232.         if (state != BattleState.PLAYERTURN)
    233.             return;
    234.  
    235.         StartCoroutine(PlayerHeal());
    236.     }
    237.  
    238. }
    small unit script is as follows:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Unit : MonoBehaviour
    6. {
    7.  
    8.     public string unitName;
    9.     public int unitLevel;
    10.  
    11.     public int strength;
    12.     public int defence;
    13.     public int agility;
    14.     public int maxHP;
    15.     public int currentHP;
    16.     public int damage;
    17.  
    18.     public bool TakeDamage(int dmg)
    19.     {
    20.         currentHP -= dmg;
    21.  
    22.         if (currentHP <= 0)
    23.         {
    24.             return true;
    25.         }
    26.         else
    27.         {
    28.             return false;
    29.         }
    30.     }
    31.  
    32.     public void Heal(int amount)
    33.     {
    34.         currentHP += amount;
    35.         if (currentHP > maxHP)
    36.         {
    37.             currentHP = maxHP;
    38.         }  
    39.     }  
    40.  
    41.  
    42. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,621
    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 ()".
     
    justicebrinston likes this.
  3. justicebrinston

    justicebrinston

    Joined:
    Oct 14, 2022
    Posts:
    3
    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.
     
  4. justicebrinston

    justicebrinston

    Joined:
    Oct 14, 2022
    Posts:
    3
    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!
     
    Last edited: Oct 14, 2022
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    That line has no meaning.

    This:

    Code (csharp):
    1. int foo = 1;
    Means "declare an integer, name it foo and set its value to 1"

    This:

    Code (csharp):
    1. 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.

    Code (csharp):
    1. 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.