In my game I use code player.GetComponent(); a lot of times, so i want to store it in the variable to make code easier. But I have error:
public GameObject player;
// A field initializer cannot reference the non-static field, method, or property
Rigidbody obj = player.GetComponent<Rigidbody>();
Edit1:
public GameObject player;
Rigidbody obj = player.GetComponent<Rigidbody>();
void Update () {
// Move Up
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
player.GetComponent<Rigidbody>().MovePosition(
player.GetComponent<Rigidbody>().position + speedUp * Time.deltaTime * playerSpeed);
//...
}