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

error CS1513: } expected

Discussion in 'Scripting' started by MariuscoGames, Jul 6, 2021.

  1. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    Hello. I am doing a script in which it is detected if the WASD keys are pressed or not. I know the script is not done right at all, or it could save resources, but it doesn't matter now. My problem is the error "Assets \ Toag.cs (56,10): error CS1513:} expected"
    How can I solve that? I have tried adding and removing symbols from {and}, but it doesn't work. Can someone send me the fixed script?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class Toag: MonoBehaviour
    7. {
    8.     void Update()
    9.     {
    10.  
    11.         if (Input.GetKey("w"))
    12.         {
    13.             Animator anim = GetComponent<Animator>();
    14.             anim.SetBool("Run", true);
    15.         }
    16.  
    17.         if (Input.GetKey("a"))
    18.         {
    19.             Animator anim = GetComponent<Animator>();
    20.             anim.SetBool("Run", true);
    21.         }
    22.      
    23.         if (Input.GetKey("d"))
    24.         {
    25.             Animator anim = GetComponent<Animator>();
    26.             anim.SetBool("Run", true);
    27.         }
    28.  
    29.         if (Input.GetKey("s"))
    30.         {
    31.             Animator anim = GetComponent<Animator>();
    32.             anim.SetBool("Run", true);
    33.  
    34.         if (Input.GetKeyUp("w"))
    35.         {
    36.             Animator anim = GetComponent<Animator>();
    37.             anim.SetBool("Run", false);
    38.         }
    39.  
    40.         if (Input.GetKeyUp("a"))
    41.         {
    42.             Animator anim = GetComponent<Animator>();
    43.             anim.SetBool("Run", false);
    44.         }
    45.      
    46.         if (Input.GetKeyUp("d"))
    47.         {
    48.             Animator anim = GetComponent<Animator>();
    49.             anim.SetBool("Run", false);
    50.         }
    51.  
    52.         if (Input.GetKeyUp("s"))
    53.         {
    54.             Animator anim = GetComponent<Animator>();
    55.             anim.SetBool("Run", false);
    56.         }
    PS: Im novice
     
  2. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    Add two } at the end of your script? Like this

    Code (CSharp):
    1. public class Toag: MonoBehaviour
    2. {
    3.     void Update()
    4.     {
    5.  
    6.         if (Input.GetKey("w"))
    7.         {
    8.             Animator anim = GetComponent<Animator>();
    9.             anim.SetBool("Run", true);
    10.         }
    11.  
    12.         if (Input.GetKey("a"))
    13.         {
    14.             Animator anim = GetComponent<Animator>();
    15.             anim.SetBool("Run", true);
    16.         }
    17.    
    18.         if (Input.GetKey("d"))
    19.         {
    20.             Animator anim = GetComponent<Animator>();
    21.             anim.SetBool("Run", true);
    22.         }
    23.  
    24.         if (Input.GetKey("s"))
    25.         {
    26.             Animator anim = GetComponent<Animator>();
    27.             anim.SetBool("Run", true);
    28.  
    29.         if (Input.GetKeyUp("w"))
    30.         {
    31.             Animator anim = GetComponent<Animator>();
    32.             anim.SetBool("Run", false);
    33.         }
    34.  
    35.         if (Input.GetKeyUp("a"))
    36.         {
    37.             Animator anim = GetComponent<Animator>();
    38.             anim.SetBool("Run", false);
    39.         }
    40.    
    41.         if (Input.GetKeyUp("d"))
    42.         {
    43.             Animator anim = GetComponent<Animator>();
    44.             anim.SetBool("Run", false);
    45.         }
    46.  
    47.         if (Input.GetKeyUp("s"))
    48.         {
    49.             Animator anim = GetComponent<Animator>();
    50.             anim.SetBool("Run", false);
    51.         }
    52.      }
    53. }
     
    Bunny83 likes this.
  3. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    Then it says
    "Assets\Toag.cs(58,2): error CS1513: } expected"
     
  4. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    Im in 2020.3.6f1
     
  5. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    There is no line 58 in your posted code, i assumed that the snippet that you've added is the entire Update(). Maybe post the entire code? According to the cs that you've posted, the issue is a missing curly brace.

    Check it out: https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs1513

    It's very common to miss something as mundane as this. However a configured IDE will help you out with spotting these issues by automatically aligning the curly braces where necessary.
     
  6. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    In your code there were no lines
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    So I added the code you gave me below that and it looked like this
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class Toag: MonoBehaviour
    7. {
    8.     void Update()
    9.     {
    10.         if (Input.GetKey("w"))
    11.         {
    12.             Animator anim = GetComponent<Animator>();
    13.             anim.SetBool("Run", true);
    14.         }
    15.         if (Input.GetKey("a"))
    16.         {
    17.             Animator anim = GetComponent<Animator>();
    18.             anim.SetBool("Run", true);
    19.         }
    20.  
    21.         if (Input.GetKey("d"))
    22.         {
    23.             Animator anim = GetComponent<Animator>();
    24.             anim.SetBool("Run", true);
    25.         }
    26.         if (Input.GetKey("s"))
    27.         {
    28.             Animator anim = GetComponent<Animator>();
    29.             anim.SetBool("Run", true);
    30.         if (Input.GetKeyUp("w"))
    31.         {
    32.             Animator anim = GetComponent<Animator>();
    33.             anim.SetBool("Run", false);
    34.         }
    35.         if (Input.GetKeyUp("a"))
    36.         {
    37.             Animator anim = GetComponent<Animator>();
    38.             anim.SetBool("Run", false);
    39.         }
    40.  
    41.         if (Input.GetKeyUp("d"))
    42.         {
    43.             Animator anim = GetComponent<Animator>();
    44.             anim.SetBool("Run", false);
    45.         }
    46.         if (Input.GetKeyUp("s"))
    47.         {
    48.             Animator anim = GetComponent<Animator>();
    49.             anim.SetBool("Run", false);
    50.         }
    51.      }
    52. }
    Sorry, but this is basically my first script
     
  7. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    This is the script that I have now:
    (won't let me edit the other post)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class Toag: MonoBehaviour
    7. {
    8.     void Update()
    9.     {
    10.         if (Input.GetKey("w"))
    11.         {
    12.             Animator anim = GetComponent<Animator>();
    13.             anim.SetBool("Run", true);
    14.         }
    15.         if (Input.GetKey("a"))
    16.         {
    17.             Animator anim = GetComponent<Animator>();
    18.             anim.SetBool("Run", true);
    19.         }
    20.  
    21.         if (Input.GetKey("d"))
    22.         {
    23.             Animator anim = GetComponent<Animator>();
    24.             anim.SetBool("Run", true);
    25.         }
    26.         if (Input.GetKey("s"))
    27.         {
    28.             Animator anim = GetComponent<Animator>();
    29.             anim.SetBool("Run", true);
    30.         if (Input.GetKeyUp("w"))
    31.         {
    32.             Animator anim = GetComponent<Animator>();
    33.             anim.SetBool("Run", false);
    34.         }
    35.         if (Input.GetKeyUp("a"))
    36.         {
    37.             Animator anim = GetComponent<Animator>();
    38.             anim.SetBool("Run", false);
    39.         }
    40.  
    41.         if (Input.GetKeyUp("d"))
    42.         {
    43.             Animator anim = GetComponent<Animator>();
    44.             anim.SetBool("Run", false);
    45.         }
    46.         if (Input.GetKeyUp("s"))
    47.         {
    48.             Animator anim = GetComponent<Animator>();
    49.             anim.SetBool("Run", false);
    50.         }
    51.      }
    52. }
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    There's only 52 lines here - so your error again makes no sense.

    Make sure you don't have any duplicate script files in your project, that you've saved the code with your changes, and that this is the same file that the error is referencing ("Assets\Toag.cs")
     
  9. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    No need to be sorry, there are a lot of learners here, including me. Yes, my bad, forgot to include that. Does it work now, or do you still get the error?
     
  10. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    Now there are 3 errors
     

    Attached Files:

    • 3.PNG
      3.PNG
      File size:
      16.6 KB
      Views:
      240
  11. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    Ok I fixed them but there are only 1 error more:
    Assets\Toag.cs(52,2): error CS1513: } expected
     
  12. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    Try adding a } symbol at the line (52,2)? Again, it seems that the snippet that you've posted is not the entire code. Judging by what you've posted it should work.
     
  13. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    I'm testing everything and now it says it has to be in (52,3) ... If it's not too much of a hassle, could you send me the complete and fixed script now?
     
  14. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    At least in my IDE it doesn't show any curly braces related issues.

    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.     using System.Collections;
    5.    
    6.     public class Toag: MonoBehaviour
    7.     {
    8.         void Update()
    9.         {
    10.             if (Input.GetKey("w"))
    11.             {
    12.                 Animator anim = GetComponent<Animator>();
    13.                 anim.SetBool("Run", true);
    14.             }
    15.             if (Input.GetKey("a"))
    16.             {
    17.                 Animator anim = GetComponent<Animator>();
    18.                 anim.SetBool("Run", true);
    19.             }
    20.    
    21.             if (Input.GetKey("d"))
    22.             {
    23.                 Animator anim = GetComponent<Animator>();
    24.                 anim.SetBool("Run", true);
    25.             }
    26.             if (Input.GetKey("s"))
    27.             {
    28.                 Animator anim = GetComponent<Animator>();
    29.                 anim.SetBool("Run", true);
    30.             if (Input.GetKeyUp("w"))
    31.             {
    32.                 Animator anim = GetComponent<Animator>();
    33.                 anim.SetBool("Run", false);
    34.             }
    35.             if (Input.GetKeyUp("a"))
    36.             {
    37.                 Animator anim = GetComponent<Animator>();
    38.                 anim.SetBool("Run", false);
    39.             }
    40.    
    41.             if (Input.GetKeyUp("d"))
    42.             {
    43.                 Animator anim = GetComponent<Animator>();
    44.                 anim.SetBool("Run", false);
    45.             }
    46.             if (Input.GetKeyUp("s"))
    47.             {
    48.                 Animator anim = GetComponent<Animator>();
    49.                 anim.SetBool("Run", false);
    50.             }
    51.          }
    52.     }
    53. }
     
  15. MariuscoGames

    MariuscoGames

    Joined:
    May 21, 2020
    Posts:
    34
    It works! Thanks!
     
    gjaccieczo likes this.
  16. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    Yay for that! Does the code editor that you use react to your mistakes? For example: try writing some random stuff in your code. Do you get the red wavy underlines?
     
  17. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,657
    You are missing a curly bracket at the end of the
    if (Input.GetKey("s"))
    .

    Also, remove the second
    using System.Collections;
    .

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Toag : MonoBehaviour
    6. {
    7.     void Update()
    8.     {
    9.         if (Input.GetKey("w"))
    10.         {
    11.             Animator anim = GetComponent<Animator>();
    12.             anim.SetBool("Run", true);
    13.         }
    14.        
    15.         if (Input.GetKey("a"))
    16.         {
    17.             Animator anim = GetComponent<Animator>();
    18.             anim.SetBool("Run", true);
    19.         }
    20.  
    21.         if (Input.GetKey("d"))
    22.         {
    23.             Animator anim = GetComponent<Animator>();
    24.             anim.SetBool("Run", true);
    25.         }
    26.        
    27.         if (Input.GetKey("s"))
    28.         {
    29.             Animator anim = GetComponent<Animator>();
    30.             anim.SetBool("Run", true);
    31.         }
    32.        
    33.         if (Input.GetKeyUp("w"))
    34.         {
    35.             Animator anim = GetComponent<Animator>();
    36.             anim.SetBool("Run", false);
    37.         }
    38.        
    39.         if (Input.GetKeyUp("a"))
    40.         {
    41.             Animator anim = GetComponent<Animator>();
    42.             anim.SetBool("Run", false);
    43.         }
    44.  
    45.         if (Input.GetKeyUp("d"))
    46.         {
    47.             Animator anim = GetComponent<Animator>();
    48.             anim.SetBool("Run", false);
    49.         }
    50.  
    51.         if (Input.GetKeyUp("s"))
    52.         {
    53.             Animator anim = GetComponent<Animator>();
    54.             anim.SetBool("Run", false);
    55.         }
    56.     }
    57. }
     
    Last edited: Jul 6, 2021
    mopthrow likes this.
  18. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    You are going to have a really hard time coming here to ask us to fix every one of your fatfinger typos.

    Let me help you be a lot more effective with tutorials.

    How to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    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.

    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!

    Unless you understand exactly what the above script does, you might want to back up and check out these tutorials:

    Imphenzia / imphenzia - super-basic Unity tutorial:



    Jason Weimann:



    Brackeys super-basic Unity Tutorial series:



    Sebastian Lague Intro to Game Development with Unity and C#:



    Imphenzia: How Did I Learn To Make Games: