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 WatcherMagic · Jun 28, 2017 at 08:52 PM · unity 52dmovementrigidbodyplayer

Unexpected symbol '=' parser error and Unexpected symbol '(' error

I've been trying to figure out the Rigidbody2D component for Unity 5, as most of the tutorials I've watched are for Unity 4. I believe I have it mostly worked out for top-down player movement, but no matter how much I fiddle around I can't get rid of these last two errors: "Error CS1519 unexpected symbol '=' / '(' in class, struct or interface member declaration." I can't find what's wrong; can anyone give a hand?

 using UnityEngine;
 using System.Collections;
 
 public class Player_Movement : MonoBehaviour {
 
     public float speed;
     rb = GetComponent<Rigidbody2D>(); //this is the line both errors always take me back to
 
 
     // Update is called once per frame
     void FixedUpdate () {
 
         //WASD Movement
         if (Input.GetKey (KeyCode.D))
             rb.AddForce (Vector2.right * speed);
         if (Input.GetKey (KeyCode.A))
             rb.AddForce (Vector2.left * speed);
         if (Input.GetKey (KeyCode.W))
             rb.AddForce (Vector2.up * speed);
         if (Input.GetKey (KeyCode.S))
             rb.AddForce (Vector2.down * speed);
 
         //Arrow keys movememt
         if (Input.GetKey (KeyCode.RightArrow))
             Player.transform.Translate(Vector2.right * speed);
         if (Input.GetKey (KeyCode.LeftArrow))
             Player.transform.Translate(Vector2.left * speed);
         if (Input.GetKey (KeyCode.UpArrow))
             Player.transform.Translate(Vector2.up * speed);
         if (Input.GetKey (KeyCode.DownArrow))
             Player.transform.Translate(Vector2.down * speed);
     }
 }

(Ignore the transform function in the arrow keys, I just haven't bothered changing that until I figure out what's going on with WASD.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jun 28, 2017 at 09:11 PM

First of all inside a class you can only "delcare" things. You can't just "execute" code. A declaration can be a field (variable), a method, a property, an event(which is a special kind of property) or a sub-type. Actual code can only be written inside of a method body or inside a field-initializer. Though field-initializer are quite limited.

In your code you have a line of arbitrary code inside a class and not inside a method. Also "rb" is not defined / declared anywhere.

Maybe you wanted to do:

 private Rigidbody2D rb = GetComponent<Rigidbody2D>();

which would declare a class field of type Rigidbody2D with the name "rb". However you can't use GetComponent inside a field-initializer. Field-initializer of instance fields can only use static methods. Field-initializers are executed before the actual constructor of a class. Instance methods are not allowed to be called before the object is fully created / initialized. You should move the GetComponent assignment into Awake or Start:

 private Rigidbody2D rb;
 
 void Awake()
 {
     rb = GetComponent<Rigidbody2D>();
 }


Comment
Add comment · Show 1 · 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 WatcherMagic · Jun 28, 2017 at 10:27 PM 0
Share

Thank you a ton, that fixed it! :)

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

210 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 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 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 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 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 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 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

weird player movement when parented to moving platform 1 Answer

2D animation transitions and unexpected position changes 2 Answers

Boss AI Help in a 2D Platform game 0 Answers

Look rotation viewing vector is zero & diagonal boosting 0 Answers

Photon Player Mover Failure 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