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.
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>();
 }
 
              Your answer