How can i destroy generated(clones) obstacles

The obstacle are generated using a generate script and gameobject is in the prefabs folder i have attached the gameobject with the generate script the problem is that it wont get destroyed

using UnityEngine;
using System.Collections;
public class Generate : MonoBehaviour
public GameObject rocks;
int scr;

	// Use this for initialization
	void Start()
		InvokeRepeating("CreateObstacle", 3f, 3f);
	void OnGui()
		GUI.color =;
		//GUILayout.Label ("Score is" + scr.ToString ());

	void CreateObstacle()

	void OnBecameInvisible() { 
		// Destroy the bullet 


you could add a ‘destoryaftertime’ script to your bullets, its what i usually do

just add a float u can edit from inspector and add that to the destroy function that u can simply call from start() or awake()


just make side camera colider and when it touch, it destroy

void OnColider(Colider col){
     if( == "ScreenSide")// That the block u placed left to the camera

The reason why its not destroying is because you are trying to destroy the original prefab. You cant destroy prefabs as its not safe to do so, in case of data loss, since its not in the scene. To destroy clones, you need to find the clone objects, either by searching for all the gameobjects called rocks(Clone) and destroying those, or you can tag all generated clones right after theyre instantiated and destroy all objects with that tag. This is just for anyone else who needs a solution for this question as the asker probably wont see it.