title: How do you reference a variable from another class?
body: <p>This is a common question and I've looked for answers that I'd actually understand for an hour and half but still no progress. It's really simple; in one class, I have a public float variable: </p>
  
  <pre><code>public class SlowOnLongGrass : MonoBehaviour
  public float speed;
  </code></pre>
  
  <p>And in another class I have : </p>
  
  <pre><code>public class SlowOnLongGrass : MonoBehaviour
  {
  
      void OnCollisionEnter2D(Collision2D col)
      {
          if (col.gameObject.tag == "Long grass")
          {
              PlayerMovement.speed -= 2f;
  </code></pre>
  
  <p>My aim : when the player walks over "long grass" reduce the speed by 2f and when the person leaves the long grass, put the speed back to its original position. However I get an error saying </p>
  
  <p>"An object reference is required for the non-static field, method or property 'PlayerMovement.speed"</p>
  
  <p>Please give a simple explanation I can understand of why my code isn't working, thanks. </p>
  
  <p>my playerMovement class : </p>
  
  <pre><code>public class PlayerMovement : MonoBehaviour
  {
  
      public Sprite Up;
      public Sprite Down;
      public Sprite Right;
      public Sprite Left;
      public float speed;
      private SpriteRenderer sr;
  
  
      // Update is called once per frame
      void Update()
      {
  
          Vector3 move;
  
          if (Input.GetKey(KeyCode.W))
          {
              GetComponent&lt;SpriteRenderer&gt;().sprite = Up;
              move = new Vector2(0, speed * Time.deltaTime);
  
              transform.position += move;
          }
          if (Input.GetKey(KeyCode.A))
          {
              GetComponent&lt;SpriteRenderer&gt;().sprite = Left;
              move = new Vector2(speed * Time.deltaTime, 0);
  
              transform.position -= move;
          }
          if (Input.GetKey(KeyCode.D))
          {
              GetComponent&lt;SpriteRenderer&gt;().sprite = Right;
              move = new Vector2(speed * Time.deltaTime, 0);
  
              transform.position += move;
          }
          if (Input.GetKey(KeyCode.S))
          {
              GetComponent&lt;SpriteRenderer&gt;().sprite = Down;
              move = new Vector2(0,speed * Time.deltaTime);
  
              transform.position -= move;
          }
  </code></pre>