Object reference is required nonstatic field, method....

Object reference is required for the non-static field method or property

Class from which I trying to get data

public class Camera_movement : MonoBehaviour {

    public GameObject player;
    public Vector3 positionen;

    // Use this for initialization
    void Start () {
    }
   
    // Update is called once per frame
    void Update () {
        Virtual_PST();
    }

    public  void Virtual_PST()
    {
        positionen = player.transform.position; //getting position of player
        Debug.Log(positionen);
    }

}

Class where I try to recieve data

public class PositionCalcView : MonoBehaviour
{
    private string posText;
    private Vector3 playerPos2;

    public Color textColor = Color.white;


    void LateUpdate()
    {
        playerPos2 = Camera_movement.Virtual_PST();    // !!!Here ERROR!!!
        posText = string.Format(DISPLAY_TEXT_FORMAT, playerPos2);
    }

Camera_movement class is non-static. You’re calling it in PositionCalvView.LateUpdate() as if there were only one instance of it (static).

Simply either define Camera_movement as a static class or point PositionCalcView to an instance of that class in the scene.

5 Likes

I solved it by making static positionen

public static Vector3 positionen;

While it works in the sort term, there are much better ways to do this.

I would start by making a public CameraMovement field on your PositionCalcView script. Then you can drag and drop the reference in the inspector.

1 Like