Player Movement Error Help

I am having some trouble with the player movement code I created. The errors I have this time are,

“Type ‘UnityEngine.Component’ does not contain a definition for ‘position’ and no extension method ‘position’ of type ‘UnityEngine.Component’ could be found. Are you missing an assembly reference?”

Could someone help me figure out why this error is occurring and how to fix it?

Thank you very much for the help!

using UnityEngine;
using System.Collections;


public class PlayerMovement : MonoBehaviour
{
    [System.Serializable]
    public class Boundary
    {
        public float xMin, xMax, yMin, yMax;
    }

    // Variables
    private float moveHorizontal;
    private float moveVertical;
    public float speed;
    public Rigidbody2D rb;
    public Boundary boundary;

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


    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
        rb.velocity = movement * speed;
        rb.position = new Vector2
        (
            Mathf.Clamp(rigidbody2D.position.x, boundary.xMin, boundary.xMax),// The error appears here and 
            Mathf.Clamp(rigidbody2D.position.y, boundary.yMin, boundary.yMax)//  it appears here as well
        );


    }
}

the reference you create to the rigidbody is “rb” not “rigidbody2D” …

1 Like

Yeah that was it. I actually had rb there at first but I got an error because something else was wrong with the script. Thanks for the help once again LeftyRighty. Learning this stuff apparently means making dumb mistakes:)