error CS1513: } expected

Hi there,
I keep receiving this error while following a tutorial, but I can’t for the life of me figure out where I’ve gone wrong.
Here’s my code! I’d really appreciate any help.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;

// Takes and handles input and movement for a player character
public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 1f;
    public float collisionOffset = 0.05f;
    public ContactFilter2D movementFilter;

    Vector2 movementInput;
    Rigidbody2D rb;
    List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();    
    }

    private void FixedUpdate() {
        // If movement input is not 0, try to move
        if(movementInput != Vector2.zero){
           bool success = TryMove(movementInput);

            if(!success) {
                success TryMove(new Vector2(movementInput.x, 0)); // If you hit a collision, see if you can slide on the x axis

                if (!success) {
                    success = TryMove(new Vector2(0, movementInput.y)); // If x axis not possible, try on the y axis
                }
            }
        }
    }
   
    private bool TryMove(Vector2 direction) {
         // Check for potential collisions
        int count = rb.Cast(
            direction, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions
            movementFilter, // The settings that determine where a collision can occur on such as layers to collide with
            castCollisions, // List of collisions to store the found collisions into after the Cast is finished
            moveSpeed * Time.fixedDeltaTime + collisionOffset); // The amount to cast equal to the movement plus an offset

        if(count == 0){ // Movement calculation
            rb.MovePosition(rb.position + direction * moveSpeed * Time.fixedDeltaTime);
            return true;
        } else {
            return false;
        }
    }

    void OnMove(InputValue movementValue) {
        movementInput = movementValue.Get<Vector2>();
    }
}

Here’s the tutorial I’m following if that’s helpful at all (I’m stuck at the section at 44:42)!

The code is the least important bit of information. The error will tell you the line/column number the compiled expected the matching bracket. That’s all it is, each “{” has to have a matching “}” and you’ve missed it.

Looking at the code style you’re using above, I’m not surprised. All those {" indented on one line and “}” on various others. Personally it’s why I don’t like using “{” on the ends of lines.

Just look through the code and ensure you have matching open/closed brackets. If in doubt, look at the tutorial code. Most IDE will show you the matching bracket if you move the cursor over one.

btw, thank you for using code-tags, most first-time posters don’t! :slight_smile:

Thanks for your response! I honestly though can’t find any missed brackets and I’ve typed it exactly the same as the tutorial. The error messages seem to find issue with lines 30-33, but in my code the colour codes show that the brackets etc are all correctly closed, so I really can’t figure out what I’m doing wrong!

Look at line 30. You’ve added “success” (missed the “=”?).

That’s just wrong so the compiler is just not understanding the intent.

OMG, you’ve solved it! Thank you so much! That’s what I get for trying to code in the wee hours, hahaha.

1 Like

Yes, happens to us all. Sometimes you have to be your own parent and tell yourself to take a break and step outside! :slight_smile:

Even veteran coders reach a point where the gray matter stops working, and we recognize it is time to go to sleep and wake up afresh, and then we instantly see the errors in our ways.

Remember, doing tutorials is far far far more than just typing precisely like a monkey. Make sure you do Step #2 below or you are completely wasting your time and effort.

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.
Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.