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?