Player Movement Script Help

I am editing my player movement code, and I broke it. Can someone help me fix it?

The errors I get are,
“An object reference is required to access non - static member ‘PlayerMovement.Boundary.xMin’”
“An object reference is required to access non - static member ‘PlayerMovement.Boundary.xMax’”
“An object reference is required to access non - static member ‘PlayerMovement.Boundary.yMin’”
“An object reference is required to access non - static member ‘PlayerMovement.Boundary.yMax’”

My code is,

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(rb.position.x, Boundary.xMin, Boundary.xMax),
            Mathf.Clamp(rb.position.y, Boundary.yMin, Boundary.yMax)
        );


    }
}

Can someone give some ideas as to what might be going wrong? Thanks for the help!

I made some edits to the code as well because I think the rb’s in the parentheses should just be rigidbody2D.

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),
            Mathf.Clamp(rigidbody2D.position.y, Boundary.yMin, Boundary.yMax)
        );


    }
}

Should be

Mathf.Clamp(rigidbody2D.position.x, boundary.xMin, boundary.xMax),
Mathf.Clamp(rigidbody2D.position.y, boundary.yMin, boundary.yMax)
1 Like