Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gdennis · Oct 23, 2014 at 11:17 PM · loadleveldontdestroyonloadduplicateawake

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Graham-Dunnett · Oct 24, 2014 at 01:09 PM

Combination of:

http://docs.unity3d.com/ScriptReference/ScriptableObject.html

and

http://docs.unity3d.com/ScriptReference/HideFlags.DontSave.html

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gdennis · Oct 25, 2014 at 09:17 AM 0
Share

I will take a look into this , thank you!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

DontDestroyOnLoad but script being excuted multiple times 1 Answer

Keeping a variable after reloading a scene 2 Answers

DontDestroyOnLoad() not calling awake/start again 2 Answers

Loading back and forth between levels, creates clone of player 1 Answer

DontDestroyOnLoad duplicate solve test failed. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges