unity 3d scripting issue

Here is my code for a cube game. I want this code to run like : when i press “Space” once, it must generate one cube. Currently its generating more than one cube for one time pressed “space” button. Secondly, when i use arrow keys it should generate cube from that position where i am standing currently, but for time being its just generating cubes from center. using UnityEngine; using System.Collections;

public class fire : MonoBehaviour {

void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == “brick” || collision.gameObject.name == “a” || collision.gameObject.name == “b”)
{
Destroy(collision.gameObject);
}
}

public float speed;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

transform.Translate(speed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, 0f);

if (Input.GetKey(KeyCode.Space))
{
    transform.Translate(speed * Vector3.up * Time.deltaTime);            
}
Vector3 temp = transform.position;
if (Input.GetKey(KeyCode.Space))
{

    GameObject textObject = (GameObject)Instantiate(Resources.Load("ball"));
}

}

You’re currently using Input.GetKey(KeyCode.Space); which returns true WHILE the key is down, so every frame, it’s returning true.

What you want to be using is either Input.GetKeyDown(KeyCode.Space); or Input.GetKeyUp(KeyCode.Space);

KeyDown and KeyUp get whether the button was pressed down or released, respectively.