Give Clones a Tag or Function

Hello. I’ve built a cube that creates clones , when you press the spacebar.I have the following code programmed:

using UnityEngine;
using System.Collections;

public class kaputt : MonoBehaviour {


	void Start () {
	
	}
	
	void Update () {
	
		if(Input.GetKey(KeyCode.Space)) {
				GameObject cube1 = GameObject.CreatePrimitive(PrimitiveType.Cube);        
				cube1.AddComponent<Rigidbody>();			
				cube1.transform.position = new Vector3(0, 5, 0);
					
		}
	}

	void OnBecameInvisible() {
		Destroy (gameObject);
	}


}

How can I clones that passed " OnBecameInvisible ( ) " so that they destroy themselves when the camera no longer has in mind ?

Many thanks

two way you can choose:

  1. use prefab that will automatically combine script with it.

  2. add this code in you if statement

cube1.AddComponent();