Don't destroy on load not working?

I randomly create a grid based world using this script. However if my player enters a building, then returns to the over world it is still recreated.

using UnityEngine;
using System.Collections;

public class CubeGrid : MonoBehaviour {
	
	public GameObject border;
	public GameObject grassLand;
	public GameObject house;
	public GameObject city;
	public GameObject tree;
	public GameObject wolf;
	public GameObject bandit;
	
	public int randNum = 0;
	public int xCubes;
	public int zCubes;
	
	public bool fighting = false;
	public bool enterWolf = false;

	void Start () {
		GenerateLandscapeAndObjects(xCubes,zCubes);
		GenerateBorder(xCubes,zCubes);
	}

	void Awake() {
		DontDestroyOnLoad(transform.gameObject);
	}
	
	private void GenerateLandscapeAndObjects(int xMax, int zMax) {
		
		//create a GameObject to contain the landscape and objects
		//to keep things neat in the Hierarchy tab if you decide
		//to start generating huge landscapes later on
		GameObject landscape = new GameObject("Landscape and Objects");


		for ( int z = 0; z < zMax; z++ )
		{
			for ( int x = 0; x < xMax; x++ )
			{
				float rnd = Random.value;
				if ( rnd < 0.01 )
				{
					DoInstantiate(city, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
				}
				else if (rnd < 0.07)
				{
					DoInstantiate(tree, new Vector3(x,0,z), Quaternion.identity,landscape.transform);
				}
				else if ( rnd < 0.15 )
				{
					DoInstantiate(house, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
				}
				else 
				{
					DoInstantiate(grassLand, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
					
					randNum = Random.Range(0,50);
					if(randNum <= 3){
						DoInstantiate(wolf,new Vector3(x,0,z), Quaternion.identity,landscape.transform);
					}
					else if(randNum ==4){
						DoInstantiate(bandit, new Vector3(x,0,z), Quaternion.identity,landscape.transform);
					}
				}
			}
		}
	}
  
	
	private void GenerateBorder(int xMax, int zMax) {
		
		//create a GameObject to contain the border
		//to keep things neat in the Hierarchy tab if you decide
		//to start generating huge landscapes later on
		GameObject borderGameObject = new GameObject("Landscape and Objects");
		//create border blocks along x axes
		for ( int x = 0; x < xMax; x++ )
		{
			DoInstantiate(border, new Vector3(x,1,0), Quaternion.identity,borderGameObject.transform);
			DoInstantiate(border, new Vector3(x,1,zMax), Quaternion.identity,borderGameObject.transform);
		}
		
		//create border blocks along z axes
		for ( int z = 0; z < zMax; z++ )
		{
			DoInstantiate(border, new Vector3(0,1,z), Quaternion.identity,borderGameObject.transform);
			DoInstantiate(border, new Vector3(xMax,1,z), Quaternion.identity,borderGameObject.transform);
		}

	}
	
	//A helper function to let you organise GameObjects easily in 
	//the hierarchy by setting the transform.parent of the instantiated prefab.
	private void DoInstantiate(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent) {
		Transform temp = ((GameObject)Instantiate(prefab,position,rotation)).transform;
		temp.parent = parent;
	}
}

Example script for being inside a house

using UnityEngine;
using System.Collections;

public class InnCameraScript : MonoBehaviour {

	void OnGUI(){
		if(GUI.Button(new Rect(10,400,1200,100), "Leave the tavern.")){
		Application.LoadLevel("Map Creator");
		}
	}
}

Can someone tell me why don’t destroy isn’t working?

Pretty old topic and its even answered but i think there is something good to mention and may help someone

DontDestroyOnLoad only works for root game objects or components of a root game object

this means that your gameObject should not have any parent object

What’s happening is another is being created. You could add this to your script to make it persistent and destroy further objects of the same type that get created on loading the scene again.

 //Needs to be static.
 private static bool spawned = false;

 void Awake()
 {
      if(spawned == false)
         {
              spawned = true;

              DontDestroyOnLoad(gameObject);

              //Code...
         }
      else
         {
               DestroyImmediate(gameObject); //This deletes the new object/s that you
                                             // mentioned were being created
         }
  }

So yeah what’s happening here is that the first time you create this, spawned is false. So the first object makes a call to the DontDestroyOnLoad(), which as the name suggests, means the first object will persist through scenes.

This is why we set the static bool spawned to true, static meaning it’s ‘shared’ between all instances of this object.

Now when you load that scene again, ‘it’s’ going to create another GameObject of this type, along with everything else in the hierarchy.

So the second time you load this scene, a second instance of the GameObject is created, however because the first GameObject set the static bool spawned to true, it’s true when the second GameObject is created, thus the else block is entered and the second object is destroyed :slight_smile:

@Razputin There is a SILLY MISTAKE here…

Let me help you and this will definitely work.

void Awake()
 {
      if(spawned == false)
         {
              spawned = true;
 
              DontDestroyOnLoad(gameObject);
 
              //Code...

               return; //<--- YOU HAVE TO PUT RETURN HERE.
               // BECAUSE once the value is true, it will go in the else condition
               //So we have to stop letting it go in the another condition.
         }
      else
         {
               Destroy(gameObject); //This deletes the new object/s that you
                                             // mentioned were being created
         }
  }

Hope it helps out.

Put it in First line of Awake function it will solve. because it need to execute first