First of all , create a new project in unity. I am using 2019.4.15f for my version here. In your project hierarchy you should see just the Main Camera and the Directional Light. Leave them as they are. Now right click on project hierarchy and from 3D Object > Cube.
With the cube selected in the window, go to Transform, and select those three dots and press reset.
Now on your project window , inside the assets folder, right click to get to the Create>Materials. Select name for the material say : CubeMaterial.
Now we will go into coding to add functionalities in the cube for it. To create a code , click on the Cube and click Add Components > New Script and type say Cube. Now double click the cube.cs script and it opens the Visual Studio for you. In the code, we will add a public variable called speed which will define the speed at which the cube rotates. For now we will set it to 10.0f; Also we will add an array of colors called color which consists of 3 colors : Red, Blue, and the green. We will also add a public MeshRenderer called as Renderer, which is used to access the materials like color of the cube at the beginning of the scene.
At the start of the screen , we want the cube to appear the certain position within the x,y and z axis. To change that lets use the transform.position to change the position to (3,4,1) . Similarly we can change the size of the cube within the code by using transform.localScale to be equal to the product of Vector3.one and 3f; So now the cube is 3by3by3 units. We can also select material property to start with the blue color. First we define material variable of type Material whose value equals to the Renderer.material. and then we set material color to be blue initially.
Now if you save your code and press play on the unity editor. You will see on the game screen with a blue static cube. But we want the cube to rotate and change color as we press the space bar. To do that we will write the rest of the code in the update portion of the Cube.cs code. So in the Update portion, for us to randomly change the color we will the Random Color selector. To do that lets create a changeColor variable of type Color . Since we have already defined mycolor array at the begining of the code, we can use that to make that selection. So we will have changeColor equals to myColor[Random.Range(0,3)].
Now to rotate the cube , we will use transform.Rotate function, and it will be as Transform.Rotate(100.0f *Time.deltaTime, 0.0f,0.0f) .
For us to change color on pressing the spacebar, we need to add the following code which checks to see if the Spacebar is pressed or not.
Now with all the additions to the code , save the code and press the play button at the top of editor and you should see a rotating cube that changes color as you press the cube.