0

I have the following code which creates a main menu :

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:

public class PreservedVariables : MonoBehaviour {

 public string filePath;

 public static PreservedVariables instance;

 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?

apxcode
  • 7,696
  • 7
  • 30
  • 41
Dennis
  • 3,044
  • 2
  • 33
  • 52
  • it's been well over 15hours I asked it there and I did not get a response there, it's a quite urgent matter so I was trying my luck here! Thanks anyway though! – Dennis Oct 24 '14 at 12:38
  • I am a bit confused by your question. LearnCocos2D and I have already tried improving it with edits. If you could narrow down the problem as well as explain it better, I could help you. That being said, I think you are `Destroying` a `GameObject` and then all hell breaks lose when it can't be found. – apxcode Oct 24 '14 at 20:10
  • Is the code after `Application.LoadLevel (0);` being executed? – Sadık Oct 25 '14 at 05:43
  • @FunctionR I am trying not to destroy that one particular gameObject because I need to retrieve the stored variable in there, but it creates duplicates of it :( Sadik yea that code is being executed because I can see the Log in my console – Dennis Oct 25 '14 at 09:09

0 Answers0