Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Stynax error, ',' expected

Discussion in 'Getting Started' started by PrabhasE, Apr 4, 2024.

  1. PrabhasE

    PrabhasE

    Joined:
    Apr 4, 2024
    Posts:
    4
    Hey I just started using unity today and would like advice/solutions to the last error I am facing as I can't seem to solve the issue no matter what tutorials or threads I listened to.

    Here are my errors and code:

    error - Assets\Scripts\PlayerController.cs(4,47): error CS1003: Stynax error, ',' expected

    code -

    using UnityEngine;
    using static System.Net.Mime.MediaTypeNames;

    public class PlayerController : MonoBehaviour;
    {
    public float speed;
    public Text countDisplay;
    public Text winText;
    private int count;
    private Rigidbody rb;

    void Start()
    {
    rb = GetComponent<Rigidbody>();
    count = 0;
    SetCountText();
    winText.text = "";
    }

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

    rb.AddForce(movement * speed * Time.deltaTime);
    }

    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag("PickUp"))
    {
    other.gameObject.SetActive(false);
    count++;
    SetCountText();
    }
    }

    void SetCountText()
    {
    countDisplay.text = "Count: " + count.ToString();
    if (count >= 12)
    {
    winText.text = "YOU WIN!";
    }
    }
    }
     
  2. PrabhasE

    PrabhasE

    Joined:
    Apr 4, 2024
    Posts:
    4
    sorry if the '{' and '}' are not alligned to what they are in the original code
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,551
    Always use the code-tag.png buttons to wrap your code in tags that will preserve formatting and make things easier to read.

    The 4 in the error message "Assets\Scripts\PlayerController.cs(4,47)" refers to the line on which the compiler got confused. It thinks a comma would be valid where it found something else. What you have on line 4 ends with a semicolon, where there should not be a semicolon. You don't need a comma, but you can't have a semicolon there.

    If you're following a tutorial, until you have some experience with C# syntax, you will need to be ultra-vigilant about every single character you type. Over time you will understand the purpose of the semicolons or braces and will make little mistakes like this less often.
     
    bugfinders likes this.
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,166
    I dont think its moaning about a comma but actually its because its a semi colon?

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour; <--
    however, one key thing, the error always tells you where it gave up trying to make sense, so its not always spot on.. but often is
     
  5. PrabhasE

    PrabhasE

    Joined:
    Apr 4, 2024
    Posts:
    4
    thanks for the advice, however i am encountering 2 additional errors once i remove the semicolon called 'Assets\Scripts\PlayerController.cs(7,17): error CS0723: Cannot declare a variable of static type 'MediaTypeNames.Text'
    Here is a screenshot of the error message:
    upload_2024-4-5_0-38-51.png
     
  6. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,166
    Well. That just means you have more things to fix.
    As a beginner its common to make stupid mistakes of missing commas, semi colons, casing of everything, spelling mistakes of even your own variables etc. however. The compiler does give you good hints on whats wrong and as it is your code you should understand what it does enough to be able to look at what the compiler says is wrong and look to fix it.

    you should not expect to come to a website every time you get it wrong and have someone fix it. Or you never truly learn

    in this case it seems you have confused it by having 2 classes called Text available and its picked one, and its problably the wrong one.
     
  7. PrabhasE

    PrabhasE

    Joined:
    Apr 4, 2024
    Posts:
    4
    Thanks for the advice, since it's my first time I probably made some dumb mistake along the way. I'll try follow the compiler and see what mistakes I can fix, thank you!