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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Missing ;

Discussion in 'Scripting' started by jonathanrhcooper, Feb 18, 2021.

  1. jonathanrhcooper

    jonathanrhcooper

    Joined:
    Dec 18, 2020
    Posts:
    15
    Capture.PNG
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.UI;
    7. using System;
    8. using System.Linq;
    9. using System.Text;
    10.  
    11. public class AmmoDisplay : MonoBehaviour
    12. {
    13.     public SerialController serialController;
    14.     public int ammo;
    15.     public bool isFiring;
    16.     public Text ammoDisplay;
    17.     public Text scoreDisplay;
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         serialController = GameObject.Find("SerialController").GetComponent<SerialController>();
    22.         StartCoroutine(Pause());
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         ammoDisplay.text = ammo.ToString();
    29.         if (Input.GetKeyDown(KeyCode.Z) && !isFiring && ammo > 0)
    30.          {
    31.             isFiring = true;
    32.             serialController.SendSerialMessage("5");
    33.             ammo--;
    34.             isFiring = false;
    35.          }
    36.  
    37.             if (Input.GetKeyUp(KeyCode.Z))
    38.             {
    39.               Debug.Log("Sending Release");
    40.               serialController.SendSerialMessage("5");
    41.             }
    42.         IEnumerator Pause()
    43.         {
    44.             if (ammo <= 0)
    45.             {
    46.                 Debug.Log("Started Coroutine at timestamp : " + Time.time);
    47.                 yeild return new WaitForSeconds(5);
    48.                 Debug.Log("Finished Coroutine at timestamp : " + Time.time);
    49.                 SceneManager.LoadScene(0);
    50.                 serialController.SendSerialMessage("4");
    51.                 Debug.Log("Sent 4");
    52.             }
    53.        
    54.         }
    55.         string message = serialController.ReadSerialMessage();
    56.         if (message == null)
    57.             return;
    58.         var chars = message.ToCharArray();
    59.         var message1 = string.Empty;
    60.         foreach (var eachChar in chars)
    61.         {
    62.             if (eachChar != ' ')
    63.             {
    64.                 message1 = message1 + eachChar;
    65.                 scoreDisplay.text = message1.ToString();
    66.             }
    67.         }
    68.     }
    69. }
    70.  
    It was good until I added in the IEnumerator Pause()

    Can someone tell me what I am missing and where?
     
    Last edited: Feb 18, 2021
  2. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Line 44, delete that semicolon after the if ;)

    Be sure to copy/paste the full error in future posts, as it may not be as easily findable without the line number.
     
  3. jonathanrhcooper

    jonathanrhcooper

    Joined:
    Dec 18, 2020
    Posts:
    15
    I noticed that myself. I removed it and still getting same error. Posted the snipping of the error above.
     
  4. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Looks like you put your Coroutine inside the Update() function by accident. Take it out :)

    Also, yield not yeild, line 47.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Also, to save time with ten million posts, wherever you're typing this in from, slow down and make sure 100% of spelling, capitalization, spacing and punctuation is perfect. Otherwise this could go on a long time.

    For your future reference, here is how to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855
     
    mopthrow likes this.
  6. jonathanrhcooper

    jonathanrhcooper

    Joined:
    Dec 18, 2020
    Posts:
    15
    Could you explain more I have tried setting it up in all setup and its still not working.
     
  7. jonathanrhcooper

    jonathanrhcooper

    Joined:
    Dec 18, 2020
    Posts:
    15
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6. using System;
    7. using System.Linq;
    8. using System.Text;
    9.  
    10. public class AmmoDisplay : MonoBehaviour
    11. {
    12.     float timeLeft = 300.0f;
    13.     public Text timer;
    14.     public SerialController serialController;
    15.     public int ammo;
    16.     public bool isFiring;
    17.     public Text ammoDisplay;
    18.     public Text scoreDisplay;
    19.     // Start is called before the first frame update
    20.  
    21.     void Start()
    22.     {
    23.         serialController = GameObject.Find("SerialController").GetComponent<SerialController>();
    24.         StartCoroutine(Pause());
    25.     }
    26.     IEnumerator Pause()
    27.     {
    28.         //Print the time of when the function is first called.
    29.         Debug.Log("Started Coroutine at timestamp : " + Time.time);
    30.  
    31.         //yield on a new YieldInstruction that waits for 5 seconds.
    32.         yield return new WaitForSeconds(5);
    33.  
    34.         //After we have waited 5 seconds print the time again.
    35.         Debug.Log("Finished Coroutine at timestamp : " + Time.time);
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update()
    40.     {
    41.         ammoDisplay.text = ammo.ToString();
    42.         if (Input.GetKeyDown(KeyCode.Z) && !isFiring && ammo > 0)
    43.          {
    44.             isFiring = true;
    45.             serialController.SendSerialMessage("5");
    46.             ammo--;
    47.             isFiring = false;
    48.          }
    49.  
    50.             if (Input.GetKeyUp(KeyCode.Z))
    51.             {
    52.               Debug.Log("Sending Release");
    53.               serialController.SendSerialMessage("5");
    54.             }
    55.         timeLeft -= Time.deltaTime;
    56.         timer.text = "Time Left:" + Mathf.Round(timeLeft);
    57.         if (timeLeft < 0)
    58.         {
    59.             SceneManager.LoadScene(0);
    60.         }
    61.             if (ammo <= 0)
    62.             IEnumerator Pause()
    63.             {
    64.                 SceneManager.LoadScene(0);
    65.                 serialController.SendSerialMessage("4");
    66.                 Debug.Log("Sent 4");
    67.             }
    68.              
    69.         string message = serialController.ReadSerialMessage();
    70.         if (message == null)
    71.             return;
    72.         var chars = message.ToCharArray();
    73.         var message1 = string.Empty;
    74.         foreach (var eachChar in chars)
    75.         {
    76.             if (eachChar != ' ')
    77.             {
    78.                 message1 = message1 + eachChar;
    79.                 scoreDisplay.text = message1.ToString();
    80.             }
    81.         }
    82.     }
    83. }
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Please read the second response from Mopthrow above.
     
    mopthrow likes this.
  9. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    This whole thing needs to be outside of Update(). It's its own function :)

    Code (CSharp):
    1.         IEnumerator Pause()
    2.         {
    3.             if (ammo <= 0)
    4.             {
    5.                 Debug.Log("Started Coroutine at timestamp : " + Time.time);
    6.                 yield return new WaitForSeconds(5);
    7.                 Debug.Log("Finished Coroutine at timestamp : " + Time.time);
    8.                 SceneManager.LoadScene(0);
    9.                 serialController.SendSerialMessage("4");
    10.                 Debug.Log("Sent 4");
    11.             }
    12.      
    13.         }
     
  10. jonathanrhcooper

    jonathanrhcooper

    Joined:
    Dec 18, 2020
    Posts:
    15
    So no errors now however, With out this consistently being checked under update, it is not wanting to check if the ammo is <= 0 so it can change scene?
     
  11. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Sure, you'd just have to change your logic a bit. Move your ammo check in to Update() instead. If the condition is met, call StartCoroutine() and let it change the scene.

    Remove that StartCoroutine in Start() if you're going to do that as you don't need to try to run it from the beginning.
     
  12. jonathanrhcooper

    jonathanrhcooper

    Joined:
    Dec 18, 2020
    Posts:
    15
    You are amazing!! I got that updated everything is working perfectly! I am starting on a new step which is to take
    Code (csharp):
    1. message1.ToString()
    and convert the number to an int so I can make an leaderboard any suggestions? And sorry about all the little dumb mistakes I have just started in unity as I am just used to Arduino programming so which is similar but a lot of differences.
     
    mopthrow likes this.
  13. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Nice! Good that you got it going :)

    For converting strings to ints theres Convert.ToInt32(), Int32.Parse(), and Int32.TryParse().

    ToInt32 and Parse will throw an exception if it fails, TryParse won't.

    Hmm this looks like something fun to try. Never heard of this.
     
    Last edited: Feb 18, 2021
  14. jonathanrhcooper

    jonathanrhcooper

    Joined:
    Dec 18, 2020
    Posts:
    15
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.UI;
    7. using System;
    8. using System.Linq;
    9. using System.Text;
    10.  
    11. public class AmmoDisplay : MonoBehaviour
    12. {
    13.     float timeLeft = 60.0f;
    14.     public Text timer;
    15.     public SerialController serialController;
    16.     public int ammo;
    17.     public bool isFiring;
    18.     public Text ammoDisplay;
    19.     public Text scoreDisplay;
    20.     public int Score = 0;
    21.     public Text Scoreint;
    22.     // Start is called before the first frame update
    23.  
    24.     void Start()
    25.     {
    26.         serialController = GameObject.Find("SerialController").GetComponent<SerialController>();
    27.     }
    28.     IEnumerator Pause()
    29.     {
    30.             Debug.Log("Started Coroutine at timestamp : " + Time.time);
    31.             yield return new WaitForSeconds(5);
    32.             Debug.Log("Finished Coroutine at timestamp : " + Time.time);
    33.             SceneManager.LoadScene(0);
    34.             serialController.SendSerialMessage("4");
    35.             Debug.Log("Sent 4");
    36.  
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update()
    41.     {
    42.  
    43.         if (ammo <= 0)
    44.         {
    45.             StartCoroutine(Pause());
    46.         }
    47.             ammoDisplay.text = ammo.ToString();
    48.         if (Input.GetKeyDown(KeyCode.Z) && !isFiring && ammo > 0)
    49.          {
    50.             isFiring = true;
    51.             serialController.SendSerialMessage("5");
    52.             ammo--;
    53.             isFiring = false;
    54.          }
    55.  
    56.             if (Input.GetKeyUp(KeyCode.Z))
    57.             {
    58.               Debug.Log("Sending Release");
    59.               serialController.SendSerialMessage("5");
    60.             }
    61.         timeLeft -= Time.deltaTime;
    62.         timer.text = "Time Left:" + Mathf.Round(timeLeft);
    63.         if (timeLeft < 0)
    64.         {
    65.             SceneManager.LoadScene(0);
    66.             serialController.SendSerialMessage("4");
    67.         }
    68.              
    69.         string message = serialController.ReadSerialMessage();
    70.         if (message == null)
    71.             return;
    72.         var chars = message.ToCharArray();
    73.         var message1 = string.Empty;
    74.         foreach (var eachChar in chars)
    75.         {
    76.             if (eachChar != ' ')
    77.             {
    78.                 message1 = message1 + eachChar;
    79.                 scoreDisplay.text = message1.ToString();
    80.             }
    81.         }
    82.         int Score = Int32.TryParse(scoreDisplay.text);
    83.         Scoreint.text = Score;
    84.     }
    85. }
    86.  
    Can you explain where the bool is coming from? And why these errors? Capture.PNG
     
  15. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    TryParse() returns a bool not an int. With TryParse()The int is stored in an out parameter.

    Code (CSharp):
    1. int score;
    2.  
    3. bool result = Int32.TryParse(text, out score);
    4.  
    5. Debug.Log(score);
    alternatively

    Code (CSharp):
    1. bool result = Int32.TryParse(text, out int score);
    2.  
    3. Debug.Log(score);