Destroyed objects don't appear on restart

Hi i am newbie in Unity and i have a problem about prefabs destruction.

I’m making a 2D infinite runner where the map is generated by a random sequel of prefab. When my character doesn’t see the old prefab, i want to destroy ( to avoid memoy explosion ).
So I used this script ( used in a tutoriel ).

I have no problem with the simulation of the game. However, when i build it, the prefabs are generated the first time but when i die and restart, nothing is generated.

I read that i have tocclone my object or something like that but i didn’t find how to do. Can you help me ? :slight_smile:

This is the script i used :

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

public class GeneratorScript : MonoBehaviour {

	public GameObject[] availableRooms;
	
	public List<GameObject> currentRooms;
	
	private float screenWidthInPoints;


	// Use this for initialization
	void Start () {
		float height = 2.0f * Camera.main.orthographicSize;
		screenWidthInPoints = height * Camera.main.aspect;
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void FixedUpdate () {
		
		GenerateRoomIfRequred();
	}


	void AddRoom(float farhtestRoomEndX)
	{
		//1
		int randomRoomIndex = Random.Range(0, availableRooms.Length);
		
		//2
		GameObject room = (GameObject)Instantiate(availableRooms[randomRoomIndex]);
		
		//3
		float roomWidth = room.transform.FindChild("floor").localScale.x;
		
		//4
		float roomCenter = farhtestRoomEndX + roomWidth * 0.5f;
		
		//5
		room.transform.position = new Vector3(roomCenter, 0, 0);
		
		//6
		currentRooms.Add(room);			
	} 

	void GenerateRoomIfRequred()
	{
		//1
		List<GameObject> roomsToRemove = new List<GameObject>();
		
		//2
		bool addRooms = true;        
		
		//3
		float playerX = transform.position.x;
		
		//4
		float removeRoomX = playerX - screenWidthInPoints;        
		
		//5
		float addRoomX = playerX + screenWidthInPoints;
		
		//6
		float farhtestRoomEndX = 0;
		
		foreach(var room in currentRooms)
		{
			//7
			float roomWidth = room.transform.FindChild("floor").localScale.x;
			float roomStartX = room.transform.position.x - (roomWidth * 0.5f);    
			float roomEndX = roomStartX + roomWidth;                            
			
			//8
			if (roomStartX > addRoomX)
				addRooms = false;
			
			//9
			if (roomEndX < removeRoomX)
				roomsToRemove.Add(room);
			
			//10
			farhtestRoomEndX = Mathf.Max(farhtestRoomEndX, roomEndX);
		}
		
		//11
		foreach(var room in roomsToRemove)
		{
			currentRooms.Remove(room);
			Destroy(room);            
		}
		
		//12
		if (addRooms)
			AddRoom(farhtestRoomEndX);
	}
}

I think my problem is with the functionForeach when i destroy room but i’m not sure.

thank you for your help but in fact i found myself the error.

When i suppress the line 95 : Destroy(room); , things seem to be ok : i’m not destroying prefabs anymore. But is it a good solution ? Because not deleting the old rooms will make crash the game no ?