Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Error CS1519

Discussion in 'Getting Started' started by santahydroix, Nov 19, 2021.

  1. santahydroix

    santahydroix

    Joined:
    Nov 19, 2021
    Posts:
    1
    Hey Guys i wan't a simple top down player movement but it will not work Help please.
    Bild_2021-11-19_215023.png Bild_2021-11-19_215055.png
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,394
    Welcome to the forums!

    The great thing about syntax errors is that the error always tells you what's going on, even if you don't understand it at first glance. So let's check out the error messages presented to you in the console. They're telling you exactly where to look, which in this case is movement.cs, line 9, columns 13 and 23.

    Both are invalid tokens, which means the compiler is saying "what you're telling me to do here doesn't make sense to me. It's having a problem with both the equals sign and the semicolon.

    So why is the compiler confused here? To answer that, you can just look at the example lines right above that point, on lines 7 and 8! Those lines are good examples of how you can handle variable declarations (and optional assignment) for a class. Let's break that down.

    Access modifier: This keyword, usually public or private, though there are other ones for slightly more advanced use cases. public means the variable can be accessed outside the class, while private means it's only accessible from within.

    Data type: This defines what type of variable you're using. It can be simple data types like float, int, or bool. It could also be a class type such as Rigidbody2D. This tells the compiler what kind of data is being stored here.

    Variable name: What do you want to call the variable so that you can reference it in your code? That's what you put here. Generally, variable names should be camelCase, while properties are PascalCase, but the compiler won't complain if you break this rule of thumb.

    Assignment (optional): If you want the variable to have a default assignment when an instance of your class is instantiated, you can use the = operator, followed by an appropriate value, to do that here.

    Okay, so now let's look at that problematic line.

    Code (CSharp):
    1. Vector2 = Movement;
    We see a data type (Vector2), but we have no access modifier. That's actually okay, since if you don't provide an access modifier, the compiler will actually assume one for you! For variables, it'll assume you want them only as private members.

    So next we have... wait -- where's the variable name! It's not there, but the compiler is absolutely expecting you to tell it what the variable should be called. That's why it says the equals sign was unexpected. It has no idea what you're trying to assign to here.

    But that's not all... After the assignment operator, you've got a value of Movement. Since your class is actually named movement (notice the difference in capitalization), I'm surprised this isn't being caught as an error, too. Maybe it would be once the other issues are resolved. But in any case, the compiler is then seeing the semicolon, which tells it this line of code is finished, and it's saying "Hey, you can't stop there, you didn't finish!"

    So... You might be thinking "Great. Tell me how to fix it!" Well, no. I'd say you'll learn a lot more by reading by long-winded explanation and fixing it yourself than you would from me just telling you how to get past this specific issue. So read this post as many times as you need to, and give it another crack.
     
    JeffDUnity3D likes this.
  3. frasderp

    frasderp

    Joined:
    Oct 6, 2016
    Posts:
    19
    Great explanation by Schneider21. I would recommend looking at the first two public variables you have declared before the Vector2 line, and consider what is different about the "structure" of these lines. You haven't declared a variable name in the Vector2 line (whereas the first two lines you have - "Speed" and "rb"). Note that the = sign is assigning the value, not a name.
     
unityunity