- Home /
How can I make the in-game menu remains unchange while changing scenes?
Hi all,
This may be asked already, but I couldn't find the right answer. I'm developing a storybook app using Unity's 2D platform, and it seems to be a platform of choice for me. As I started developing it, I have the main menu done no problem and adding new scenes is fine too. But I got to the part where I want the previous page button, next page button, and options menu (to adjust FX and/or music volume) that I have created for the app in the first scene to remain on every scene. I know that every scene has its own camera, thus I can only create these same buttons and menus on every scene. Is there any other way to create them only one time and reuse them on other scenes?
Thank you!
Answer by edcarlo · Feb 03, 2014 at 07:29 AM
You should create a gameobject with a class that holds the values of settings and add "DontDestroyOnLoad(this.gameObject)" inside Awake method.
public class SettingsValue : MonoBe...{
public float sfxVolume, musicVolume;
void Awake()
{
DontDestroyOnLoad(gameObject);
if (GameObject.FindObjectsOfType(GetType()).Length > 1)
{
Destroy(this.gameObject);
}
}
}
You should set the values in your scene where you have the gui menus.
Hi edcarlo,
I found this topic "Objects being duplicated with DontDestroyOnLoad". Basically this exactly is my problem. I could now use DontDestroyOnLoad on buttons on my first scene, but they duplicated themselves. So I found this code:
public void Awake()
{
DontDestroyOnLoad(this);
if (FindObjectsOfType(GetType()).Length > 1)
{
Destroy(gameObject);
}
}
But I don't know where should I put it. A separate script or inside the LoadLevelOnClick? Sorry, I'm new to scripting.
just add below of DontDestroyOnLoad..
you should put it at the same class
Ok, tried that with the same class and it still got destroyed.
using UnityEngine;
[AddComponent$$anonymous$$enu("NGUI/Examples/Load Level On Click")]
public class LoadLevelOnClick : $$anonymous$$onoBehaviour
{
public string levelName;
void OnClick ()
{
if (!string.IsNullOrEmpty(levelName))
{
Application.LoadLevel(levelName);
}
}
public void Awake()
{
DontDestroyOnLoad(this);
if (FindObjectsOfType(GetType()).Length > 1)
{
Destroy(gameObject);
}
}
}
I did this and it works, only that my LoadLevelOnClick script is not working after it does this:
public void Awake()
{
DontDestroyOnLoad(transform.root.gameObject);
if (FindObjectsOfType(GetType()).Length > 1)
{
Destroy(transform.root.gameObject);
}
}
Answer by ffxz7ff · Feb 03, 2014 at 06:17 AM
Is this the hourly http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html thread?
Answer by adissapong · Feb 03, 2014 at 06:58 AM
I tried this by attached the JS script on a button on my first scene. It still got deleted.