Moving object

My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cube : MonoBehaviour {

private Rigidbody rigidbody;
// Use this for initialization
void Start ()
{

}

// Update is called once per frame
void Update ()
{
if (Input.GetKey (KeyCode.UpArrow))
{
rigidbody.AddForce (Vector2.up * 2000);
}
}
}

Problem in 7th line (private Rigidbody rigidbody;):

“no value will ever be assigned to the “Cube.Rigidbody” field and the default value of this field will always be used: null.”

I dont know what should I do, cube does not move when I pressing up arrow, can someone help me?

As the error is trying to say, you need to initialize your variable. If you’re confused about this in the future go look at the example in the manual: Unity - Scripting API: Rigidbody.AddForce

void Start ()
{
  rigidbody = GetComponent<Rigidbody>();
}

Additionally, applying forces to a rigidbody in Update is a bad idea. You want to capture the input in Update but apply the force in FixedUpdate.

thank you now this works, but I have one problem. my cube is rolling and changing rotation and I dont know what should I do, I tried to do this with sphere, but this is rolling too.

A good place to start would be to describe exactly what behavior you are trying to accomplish.