Delete duplicate gameobject on restart

Hey, I am new to Unity.
I have the following code which creates a main menu :

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class EscapeGUI : MonoBehaviour {

public GUISkin MySkin;
public bool pauseToggle = false;
public bool showGUI = false;
public bool levelLoaded = false;

static string filePath;

private List<string> list = new List<string>();
private string line;

void Update() {
	if (!levelLoaded) {
		showGUI = true;
		Time.timeScale = 0;
		Debug.Log ("NO LEVEL LOADED");
	} else {
		if (Input.GetKeyDown (KeyCode.Escape)) {
			pauseToggle = !pauseToggle;
			if (pauseToggle) {
					Time.timeScale = 0;
					showGUI = true;
			} else {
					Time.timeScale = 1;
					showGUI = false;
			}
		}

		Debug.Log("FILEPATH IS " + filePath);
		Debug.Log("LEVEL IS LOADED");
	}
}

void OnGUI() {
	if (showGUI) {

		GUI.skin = MySkin;
		GUILayout.BeginArea (new Rect (Screen.width / 4, Screen.height / 4, 400, Screen.width / 2));

		GUILayout.BeginHorizontal ();
		if (levelLoaded){
			if (GUILayout.Button ("Resume")) {
				Time.timeScale = 1;
				showGUI = false;
				pauseToggle = false;
			}
		}
		GUILayout.EndHorizontal ();
		
		GUILayout.BeginHorizontal ();
		if (levelLoaded){
			if (GUILayout.Button ("Restart")) {
				Application.LoadLevel (0);
				showGUI = false;
				pauseToggle = false;
				Time.timeScale = 1;
				levelLoaded = true;
				Debug.Log ("Game is restarted with this level: " + filePath);
			}
		}
		GUILayout.EndHorizontal ();
		
		GUILayout.BeginHorizontal ();
		if (GUILayout.Button ("Load")) {
			filePath = EditorUtility.OpenFilePanel("Select JSON file",Application.streamingAssetsPath,"txt");
			Debug.Log ("Game is loaded with this level: " + filePath);

			StreamReader reader = new StreamReader(filePath);
			while ((line = reader.ReadLine()) != null)
			{
				list.Add(line);
				//Debug.Log(line);
			}

			//Do this as soon as the JSON is checked and found to be OK.
			GameObject.Find("Preserved Variables").SendMessage("setFilePath", filePath);

			Time.timeScale = 1;
			levelLoaded = true;
			showGUI = false;
			pauseToggle = false;
		}
		GUILayout.EndHorizontal ();
		
		GUILayout.BeginHorizontal ();
		if (GUILayout.Button ("Quit")) {
			Application.Quit();
		}
		GUILayout.EndHorizontal ();
		
		GUILayout.EndArea ();
	}
}

}

A game is created by importing a JSON file (in the code its just txt for testing, i have not implemented the JSON part yet), in this JSON file the game flow will be described.

So basically when a player clicks load, I want the game to be playable and then when he clicks restart because of the ‘Application.LoadLevel (0);’ code everythings gets deleted, thus I don’t know anymore what the current file (level) it was.

So I created an empty gameobject called ‘Preserved Variables’ and I put a C# script component in this, the script looks like this:

using UnityEngine;
using System.Collections;

public class PreservedVariables : MonoBehaviour {

public string filePath;

public static PreservedVariables instance;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void Awake() {
	if(instance){
		Destroy(this);
	} else {
		DontDestroyOnLoad(this);
		instance = this;
	}
}

void setFilePath(string fp) {
	filePath = fp;
}

string getFilePath() {
	return filePath;
}

}

Now the problem is that when I run this and ingame i click ‘load’, i select my file and so far everything is good. But then when I click “restart” I get the following 2 problems:

  1. the main menu shows me only the ‘load’ and ‘quit’ as it is only supposed to show when there is no game loaded (so this only happens at startup), however i think this will be fixed by fixing nr.2 (see below)

  2. As soon as i click restart after loading a file, the gameobject ‘Preserved Variables’ is made again but this time it does not have a script component attached. (the original gameobject has its FilePath updated correctly though).

If I may I would like to ask an extra small question to this, how do I exactly retrieve the filepath variable again from the empty gameobject ‘Preserved Variables’ so that I can use it in my restart code?

Sorry for the big post, I hope someone can help me!!

Thanks in advance,
Dennis

Combination of:

and