Search Unity

Bug Expected comma error

Discussion in 'Scripting' started by Derek9132, Mar 26, 2023.

  1. Derek9132

    Derek9132

    Joined:
    Mar 26, 2023
    Posts:
    3
    Hello all,

    This is my first time programming player movement into unity and I am already encountering errors I do not understand. I'm receiving 6 errors, all of them saying ',' expected on various lines. What confuses me most is that sometimes the place where the compiler says the error is does not exist. For example, I believe (7,30) means line 7 character 30, but line 7 only has 24 characters. I have attached a picture as well as some txt files of my code. If someone could correct my code and give me an explanation I would be eternally grateful.

    Also: I do not think the errors are in the InputManager nor PlayerLocomotion since I can add them as components just fine.
     

    Attached Files:

  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,861
    Empty spaces count as characters.

    Also in future, post the scripts in the body of your post in code tags. One look at your player manager and I can immediately see your syntax is invalid:

    Code (CSharp):
    1. public class PlayerManager : MonoBehaviour
    2.  
    3.     InputManager inputManage;
    4.     PlayerLocomotion playermove;
    5. {
    Your field declarations should be after the opening curly brace of your class body.

    If you're still learning, please follow C# tutorials to get the hang of the syntax, as you have to be 100% accurate.
     
    Bunny83 likes this.
  3. Derek9132

    Derek9132

    Joined:
    Mar 26, 2023
    Posts:
    3
    I am so sorry, I had the field declarations in the right place for my other classes but I completely overlooked them for the PlayerManager class. Thank you so much for being the extra pair of eyes to spot what I couldn't see. Sorry again for such an avoidable mistake.
     
    spiney199 likes this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,993
    Right. One of the most important concept for new programmers is understanding the concept of scope and what those curly braces actually mean / represent. A lot declarations (like a class declaration) have a header and a body scope. The body has to follow the header.

    What's also important to keep in mind: The compiler is stupid. It does not understand your intend, at all. It reads your file from top to bottom, from left to right, character by character. When you get multiple errors, ALWAYS address the first error. In many cases a syntax error in a line will completely throw off the structure of any code that may follow. So fixing the first error would solve all the following errors at once. For 99% of all compiler errors, the error has to be either at the exact line / position the compiler says, or somewhere above.

    Also note that for most of your code the compiler does not care about lines. New lines are mainly a tool to make it more human readable. You could write almost all code into a single line and the compiler doesn't care. It makes no difference for the compiler. The only things that actually require new lines are line comments, preprocessor tags or
    #regions
    . Those are terminated by the end of the line they are declared in. Pretty much anything else could be written in a single line. Hopefully we agree that writing code like this:
    Code (CSharp):
    1. using System.Collections;using System.Collections.Generic;using UnityEngine;public class PlayerManager : MonoBehaviour{ InputManager inputManage; PlayerLocomotion playermove; public void Awake(){ inputManage = GetComponent<InputManager>(); playermove = GetComponent<PlayerLocomotion>(); } private void Update() { inputManage.HandleAll(); } private void FixedUpdate(){ playermove.HandleAllMovement(); } }
    is not a good idea. Though as I said, the compiler doesn't care. Statements are terminated by a semicolon, so you "could" just place them one after the other. In fact you are. New line characters are also just white space characters that the compiler actually ignores (with the few exceptions I mentioned above).
     
    spiney199 likes this.