Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Frepperino · Oct 24, 2015 at 11:46 AM · parsing error

What does {} do and when to use them (Beginner)

Hello everyone, I'm trying to make a top down game/game prototype and I've just began with scripting. So I've seen some 2D top down tutorials and took the D movement of a tutorial trying to replicate it with W but when I save my script and try to play it, it says "Parsing Error". I suppose it means that I need to use this symbol "{}" in some way but I don't understand the symbol at all.

I see these "{}" symbols in every script but I don't understand how they work, what they do or even when to use them.

My script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMobility : MonoBehaviour {
 
     public float speed;
 
     void Update () {
     
         if (Input.GetKey (KeyCode.D))
             //transform.Translate (Vector2.right * speed);
             //transform.position += Vector3.right * speed;
         if (Input.GetKey (KeyCode.W))
             //transform.Translate (Vector2.up * speed);
             //transform.position += Vector3.up * speed;
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spooneystone · Oct 24, 2015 at 12:05 PM 1
Share

The best thing for you to do really is take a step out of unity for a moment and focus on some c# tutorials.

avatar image Frepperino spooneystone · Oct 24, 2015 at 12:07 PM 0
Share

Yeah I guess your right, thanks for the fast reply!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Statement · Oct 24, 2015 at 12:04 PM

Simplified, they define scopes. Think of them as "from here" and "to here" markers.

 public class MyClass
 { // MyClass body starts here
     
     public void MyMethod()
     { // MyMethod body starts here
     
         if (true)
         { // statement block starts here

              // Code...
              // Code...
              // Code...

         } // statement block ends here
     
     } // MyMethod body ends here
     
 } // MyClass body ends here

C# Language Specification (.NET 2003 docs, so somewhat old but still relevant)

  • Class body

  • Method body

  • Statement blocks

Your code is missing the body terminator for your class and method, and you want to create a statement block after you if, because you have more than one statement.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMobility : MonoBehaviour
 {
     public float speed;
 
     void Update()
     {
         // After if (expression) follows a statement, or a statement block
         if (Input.GetKey(KeyCode.D)) 
         // Statement "block", containing multiple statements
         { 
             transform.Translate(Vector2.right * speed);  // One statement
             transform.position += Vector3.right * speed; // Another statement
         }
         if (Input.GetKey(KeyCode.W))
         {
             transform.Translate(Vector2.up * speed);
             transform.position += Vector3.up * speed;
         }
     }
 }

Since your code appear to do the same thing twice, we can get rid of one statement in each block and thus simplify the code by removing the block entirely.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMobility : MonoBehaviour
 {
     public float speed;
 
     void Update()
     {
         // After if (expression) follows a statement, or a statement block
         if (Input.GetKey(KeyCode.D)) 
             transform.Translate(Vector2.right * speed);  // One statement

         if (Input.GetKey(KeyCode.W))
             transform.Translate(Vector2.up * speed);
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Oct 24, 2015 at 12:07 PM 0
Share

Eh, the links are not very useful. If you are interested in learning more, search for C# 3.0 Language Specification. There are docs that describe it in detail.

avatar image Frepperino · Oct 24, 2015 at 12:08 PM 0
Share

Thanks for explaining in such a short period of time!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Parsing error, 1 Answer

Hi everyone, stupid me accidently deleted my script and I had to write it again and now there are problems. If someone could help me it would be much appreciated 3 Answers

Invalid PBX project - iOS Build Help! 2 Answers

error CS8025: Parsing error 1 Answer

Parsing error 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges