Search Unity

I need help with C# can anyone help please

Discussion in 'Getting Started' started by Mackerel67, May 7, 2020.

  1. Mackerel67

    Mackerel67

    Joined:
    May 6, 2020
    Posts:
    11
    Hello, can someone please help me with this error, any help is greatly appreciated.
    Kind Regards, Mackerel67

    Assets\Scripts\GameManager.cs(60,40): error CS1001: identifier expected


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class GameManager : MonoBehaviour{

    public delegate void GameDelegate();
    public static event GameDelegate OnGameStarted;
    public static event GameDelegate OnGameOverConfirmed;


    public static GameManager Instance;

    public GameObject StartPage;
    public GameObject GameOverPage;
    public GameObject CountdownPage;
    public Text ScoreText;

    enum PageState {
    None,
    Start,
    GameOver,
    Countdown
    }

    int score = 0;
    bool gameOver = false;

    public bool GameOver {get{return gameOver; } }

    void Awake() {
    Instance = this;
    }

    void OnEnable() {
    CountdownText.OnCountdownFinished += OnCountdownFinished;
    TapController.OnPlayerDied += OnPlayerDied;
    TapController.OnPlayerScored += OnPlayerScored;
    }

    void OnDisable() {
    CountdownText.OnCountdownFinished -= OnCountdownFinished;
    TapController.OnPlayerDied -= OnPlayerDied;
    TapController.OnPlayerScored -= OnPlayerScored;
    }

    void OnCountdownFinished() {
    SetPageState(PageState.None);
    OnGameStarted();
    score = 0;
    GameOver = false;
    }

    void OnPlayerDied() {
    GameOver = true;
    int savedScore = PlayerPrefs.GetInt("HighScore");
    if (score > savedScore) NewMethod();
    PlayerPrefs.SetInt("HighScore", score);
    }
    //60-----> SetPageState(PageState.GameOver);
    }

    void OnPlayerScored() {
    score++;
    ScoreText.text = score.ToString();
    }
    void SetPageState(PageState state) {
    switch (state) {
    case PageState.None:
    StartPage.SetActive(false);
    GameOverPage.SetActive(false);
    CountdownPage.SetActive(false);
    break;
    case PageState.Start:
    StartPage.SetActive(true);
    GameOverPage.SetActive(false);
    CountdownPage.SetActive(false);
    break;
    case PageState.GameOver:
    StartPage.SetActive(false);
    GameOverPage.SetActive(true);
    CountdownPage.SetActive(false);
    break;
    case PageState.Countdown:
    StartPage.SetActive(false);
    GameOverPage.SetActive(false);
    CountdownPage.SetActive(true);
    break;
    }
    }

    public void ConfirmGameOver() {
    //activated when replay button is hit
    //OnGameOverConfirmed(); //event
    ScoreText.text = "0";
    SetPageState(PageState.Start);
    }

    public void StartGame() {
    //activated when play button is hit
    SetPageState(PageState.Countdown);
    }
     

    Attached Files:

  2. Mackerel67

    Mackerel67

    Joined:
    May 6, 2020
    Posts:
    11
    Keep in mind i am a rookie, and i didn't make up this script, i got it of a tutorial, if you want to know what the tutorial is just follow this link:
    and thank you again for all your help :)
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Try editing your message, and pasting your code again using the "Code" button in the editing toolbar. Then it'll be much more readable (and will have line numbers too, which will be helpful since problem is on line 60 or thereabouts).
     
    Joe-Censored and Noblauch like this.
  4. Mackerel67

    Mackerel67

    Joined:
    May 6, 2020
    Posts:
    11
    I am sorry but i am completely lost; have no idea where the "Code" button is in the editing toolbar, and thank you for your time, any help is needed. :)
     
  5. Mackerel67

    Mackerel67

    Joined:
    May 6, 2020
    Posts:
    11
    And if this is any help i'm using visual studios
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It's right next to the word "Code:".
    upload_2020-5-8_7-31-45.png
     
  7. Mackerel67

    Mackerel67

    Joined:
    May 6, 2020
    Posts:
    11
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class GameManager : MonoBehaviour{
    6.  
    7. public delegate void GameDelegate();
    8. public static event GameDelegate OnGameStarted;
    9. public static event GameDelegate OnGameOverConfirmed;
    10.  
    11.  
    12. public static GameManager Instance;
    13.  
    14. public GameObject StartPage;
    15. public GameObject GameOverPage;
    16. public GameObject CountdownPage;
    17. public Text ScoreText;
    18.  
    19. enum PageState {
    20. None,
    21. Start,
    22. GameOver,
    23. Countdown
    24. }
    25.  
    26. int score = 0;
    27. bool gameOver = false;
    28.  
    29. public bool GameOver {get{return gameOver; } }
    30.  
    31. void Awake() {
    32. Instance = this;
    33. }
    34.  
    35. void OnEnable() {
    36. CountdownText.OnCountdownFinished += OnCountdownFinished;
    37. TapController.OnPlayerDied += OnPlayerDied;
    38. TapController.OnPlayerScored += OnPlayerScored;
    39. }
    40.  
    41. void OnDisable() {
    42. CountdownText.OnCountdownFinished -= OnCountdownFinished;
    43. TapController.OnPlayerDied -= OnPlayerDied;
    44. TapController.OnPlayerScored -= OnPlayerScored;
    45. }
    46.  
    47. void OnCountdownFinished() {
    48. SetPageState(PageState.None);
    49. OnGameStarted();
    50. score = 0;
    51. GameOver = false;
    52. }
    53.  
    54. void OnPlayerDied() {
    55. GameOver = true;
    56. int savedScore = PlayerPrefs.GetInt("HighScore");
    57. if (score > savedScore) NewMethod();
    58. PlayerPrefs.SetInt("HighScore", score);
    59. }
    60. SetPageState(PageState.GameOver);
    61. }
    62.  
    63. void OnPlayerScored() {
    64. score++;
    65. ScoreText.text = score.ToString();
    66. }
    67. void SetPageState(PageState state) {
    68. switch (state) {
    69. case PageState.None:
    70. StartPage.SetActive(false);
    71. GameOverPage.SetActive(false);
    72. CountdownPage.SetActive(false);
    73. break;
    74. case PageState.Start:
    75. StartPage.SetActive(true);
    76. GameOverPage.SetActive(false);
    77. CountdownPage.SetActive(false);
    78. break;
    79. case PageState.GameOver:
    80. StartPage.SetActive(false);
    81. GameOverPage.SetActive(true);
    82. CountdownPage.SetActive(false);
    83. break;
    84. case PageState.Countdown:
    85. StartPage.SetActive(false);
    86. GameOverPage.SetActive(false);
    87. CountdownPage.SetActive(true);
    88. break;
    89. }
    90. }
    91.  
    92. public void ConfirmGameOver() {
    93. //activated when replay button is hit
    94. //OnGameOverConfirmed(); //event
    95. ScoreText.text = "0";
    96. SetPageState(PageState.Start);
    97. }
    98.  
    99. public void StartGame() {
    100. //activated when play button is hit
    101. SetPageState(PageState.Countdown);
    102. }
     
  8. Sommer_

    Sommer_

    Joined:
    Apr 23, 2020
    Posts:
    18
    it looks like you have an extra bracket on line 59.. try removing it
     
    JoeStrout likes this.
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It also looks like your code isn't indented; perhaps you copied it from your original post, rather than from the code editor? Any code editor will automatically indent it for you, and that indentation is useful because it shows you how the compiler is going to interpret the code. In this case, the extra bracket on line 59 should jump right out at you because everything after that has incorrect indentation.
     
    Sommer_ likes this.
  10. Mackerel67

    Mackerel67

    Joined:
    May 6, 2020
    Posts:
    11
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class GameManager : MonoBehaviour{
    6.  
    7.     public delegate void GameDelegate();
    8.     public static event GameDelegate OnGameStarted;
    9.     public static event GameDelegate OnGameOverConfirmed;
    10.  
    11.  
    12.     public static GameManager Instance;
    13.  
    14.     public GameObject StartPage;
    15.     public GameObject GameOverPage;
    16.     public GameObject CountdownPage;
    17.     public Text ScoreText;
    18.  
    19.     enum PageState {
    20.         None,
    21.         Start,
    22.         GameOver,
    23.         Countdown
    24.     }
    25.  
    26.     int score = 0;
    27.     bool gameOver = false;
    28.  
    29.     public bool GameOver {get{return gameOver; } }
    30.  
    31.     void Awake() {
    32.         Instance = this;
    33.     }
    34.    
    35.     void OnEnable() {
    36.         CountdownText.OnCountdownFinished += OnCountdownFinished;
    37.         TapController.OnPlayerDied += OnPlayerDied;
    38.         TapController.OnPlayerScored += OnPlayerScored;
    39.     }
    40.  
    41.     void OnDisable() {
    42.         CountdownText.OnCountdownFinished -= OnCountdownFinished;
    43.         TapController.OnPlayerDied -= OnPlayerDied;
    44.         TapController.OnPlayerScored -= OnPlayerScored;
    45.     }
    46.  
    47.     void OnCountdownFinished() {
    48.         SetPageState(PageState.None);
    49.         OnGameStarted();
    50.         score = 0;
    51.         GameOver = false;
    52.     }
    53.  
    54.     void OnPlayerDied() {
    55.          GameOver = true;
    56.          int savedScore = PlayerPrefs.GetInt("HighScore");
    57.         if (score > savedScore) NewMethod();
    58.             PlayerPrefs.SetInt("HighScore", score);
    59.         }
    60.         SetPageState PageState(GameOver);
    61.        
    62.     }
    63.  
    64.     void OnPlayerScored() {
    65.         score++;
    66.         ScoreText.text = score.ToString();
    67.     }
    68.     void SetPageState(PageState state) {
    69.         switch (state) {
    70.             case PageState.None:
    71.                 StartPage.SetActive(false);
    72.                 GameOverPage.SetActive(false);
    73.                 CountdownPage.SetActive(false);
    74.                 break;
    75.             case PageState.Start:
    76.                 StartPage.SetActive(true);
    77.                 GameOverPage.SetActive(false);
    78.                 CountdownPage.SetActive(false);
    79.                 break;
    80.                 case PageState.GameOver:
    81.                 StartPage.SetActive(false);
    82.                 GameOverPage.SetActive(true);
    83.                 CountdownPage.SetActive(false);
    84.                 break;
    85.                 case PageState.Countdown:
    86.                 StartPage.SetActive(false);
    87.                 GameOverPage.SetActive(false);
    88.                 CountdownPage.SetActive(true);
    89.                 break;
    90.         }
    91.     }
    92.  
    93.     public void ConfirmGameOver() {
    94.         //activated when replay button is hit
    95.         //OnGameOverConfirmed(); //event
    96.         ScoreText.text = "0";
    97.         SetPageState(PageState.Start);
    98.     }
    99.  
    100.     public void StartGame() {
    101.         //activated when play button is hit
    102.         SetPageState(PageState.Countdown);
    103.     }
    104.  
    105.  
     
  11. Mackerel67

    Mackerel67

    Joined:
    May 6, 2020
    Posts:
    11
    i'm pretty sure that this is indented because this time i copied and pasted it from my code editor
     
  12. Mackerel67

    Mackerel67

    Joined:
    May 6, 2020
    Posts:
    11
    hello? the extra bracket was unfortunately not the problem
     
  13. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Well, it's not. It looks the same as the first time you pasted it.

    Here is your code indented:
    Code (CSharp):
    1. public class GameManager : MonoBehaviour {
    2.  
    3.     public delegate void GameDelegate();
    4.     public static event GameDelegate OnGameStarted;
    5.     public static event GameDelegate OnGameOverConfirmed;
    6.  
    7.  
    8.     public static GameManager Instance;
    9.  
    10.     public GameObject StartPage;
    11.     public GameObject GameOverPage;
    12.     public GameObject CountdownPage;
    13.     public Text ScoreText;
    14.  
    15.     enum PageState {
    16.         None,
    17.         Start,
    18.         GameOver,
    19.         Countdown
    20.     }
    21.  
    22.     int score = 0;
    23.     bool gameOver = false;
    24.  
    25.     public bool GameOver { get { return gameOver; } }
    26.  
    27.     void Awake() {
    28.         Instance = this;
    29.     }
    30.  
    31.     void OnEnable() {
    32.         CountdownText.OnCountdownFinished += OnCountdownFinished;
    33.         TapController.OnPlayerDied += OnPlayerDied;
    34.         TapController.OnPlayerScored += OnPlayerScored;
    35.     }
    36.  
    37.     void OnDisable() {
    38.         CountdownText.OnCountdownFinished -= OnCountdownFinished;
    39.         TapController.OnPlayerDied -= OnPlayerDied;
    40.         TapController.OnPlayerScored -= OnPlayerScored;
    41.     }
    42.  
    43.     void OnCountdownFinished() {
    44.         SetPageState(PageState.None);
    45.         OnGameStarted();
    46.         score = 0;
    47.         GameOver = false;
    48.     }
    49.  
    50.     void OnPlayerDied() {
    51.         GameOver = true;
    52.         int savedScore = PlayerPrefs.GetInt("HighScore");
    53.         if(score > savedScore) NewMethod();
    54.         PlayerPrefs.SetInt("HighScore", score);
    55.     }
    56.     SetPageState(PageState.GameOver);
    57. } //<--------------------------------------------------------- The culprit
    58.  
    59. void OnPlayerScored() {
    60.     score++;
    61.     ScoreText.text = score.ToString();
    62. }
    63. void SetPageState(PageState state) {
    64.     switch(state) {
    65.         case PageState.None:
    66.             StartPage.SetActive(false);
    67.             GameOverPage.SetActive(false);
    68.             CountdownPage.SetActive(false);
    69.             break;
    70.         case PageState.Start:
    71.             StartPage.SetActive(true);
    72.             GameOverPage.SetActive(false);
    73.             CountdownPage.SetActive(false);
    74.             break;
    75.         case PageState.GameOver:
    76.             StartPage.SetActive(false);
    77.             GameOverPage.SetActive(true);
    78.             CountdownPage.SetActive(false);
    79.             break;
    80.         case PageState.Countdown:
    81.             StartPage.SetActive(false);
    82.             GameOverPage.SetActive(false);
    83.             CountdownPage.SetActive(true);
    84.             break;
    85.     }
    86. }
    87.  
    88. public void ConfirmGameOver() {
    89.     //activated when replay button is hit
    90.     //OnGameOverConfirmed(); //event
    91.     ScoreText.text = "0";
    92.     SetPageState(PageState.Start);
    93. }
    94.  
    95. public void StartGame() {
    96.     //activated when play button is hit
    97.     SetPageState(PageState.Countdown);
    98. }
    Everything starting from the "OnPlayerScored" method downwards is outside of your GameManager class.
    Simply move the closing curly brace for this class to the end of the file.
     
    JoeStrout and Ryiah like this.
  14. Mackerel67

    Mackerel67

    Joined:
    May 6, 2020
    Posts:
    11
    I did that but for some reason there the second bracket in line 60 is underlined in red
     
  15. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    It should be able to compile.
    What's the error message?