Unity - create a common menu for multiple scenes - unity3d

I've created a 3D game with 10 levels (10 scenes) with unity 2018. In the first scene I have added the canvas which displays the menu on click of ESC button.
But the problem is the menu is displayed only in the first level(scene). For the menu to display in other scenes I need to copy the same menu canvas to other scenes as well.
I am looking for a technique to have a common menu canvas for all the scenes without repeating it in all the scenes. Is there any way to do it?

Make it persist using DontDestroyOnLoad
void Awake()
{
DontDestroyOnLoad(gameObject);
}

You could use LoadScene.Additive, and have the Escape-Menu in a separate scene. The different scenes can then also be loaded and unloaded as necessary by some "master entity". I'm not sure if this is actually an antipattern, though.
Another option would simply be to make the menu a prefab and to (once) manually drag it into each scene.

Related

Why are gameobjects not visible in the Scene Tab but visible in the Game Tab?

I am working on a Unity project. I save it, and then I open more later, and now it's not showing the objects in the Scene tab.
However, in the Game tab, it does.
I read these forums, and the majority think it's because objects are not in the same layer option or something with the camera.
Links here , here and this.
Here are a Gameobject that dont showing in Scene Tab. It's a rectangle:
My main camera is working well, showing all the objects correctly:
All gameobjects are set up in the "Everything" option:

why am I losing all gameObjects in unity when I have more then one scene

I am trying to create a tower defense game and I made a menu scene and a level01 scene.
Now when I create some objects in the menu scene, and go to the level01 scene, I lose all my objects in my menu scene. I don't know what is going on. I have tried to create a new menu scene and duplicating my level01 scene, but it doesn't work.
You can use DontDestroyOnLoad or you can add multiple scenes and never unload the menu scene.
SceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive);
The use of a scene, as its name implies, is to prescribe a different scene and arrangement. If you can design the gameplay inside a scene and deactivate them beforehand instead of adding objects. This will make the code more consistent and will prevent future problems.
You can mark objects to keep them between loading scenes by DontDestroyOnLoad(myObject). Here is the documentation with sample code:
Object.DontDestroyOnLoad

how to switch between loaded scenes

I am new to unity and working on a projects.
I want to work with multiple scenes.
some of my scenes are like option menu in the game.
from my main screen I want to open an options scene and when I am done I want to move back to my main scene and when I am back I want to keep the things done in main scene before the options scene opens
I can change scenes with SceneManager but it loads the screen as new as if I did nothing there
is it possible to switch between loaded scenes without loading again? I think that if it is; I can continue from the progress in the main scene
if it is not how can I continue my progress (do I have to keep all the data and when the scene starts load back from that data? )
The SceneManager class provides lots of useful ways to manage your scenes. You can find documentation here.
Using multiple scenes to separate your logic is a great approach, and using the LoadSceneMode.Additive option when loading a new scene lets you load one scene alongside another.
To achieve what you want, you'd roughly need to do the following:
Load your main menu scene.
Load your options scene additively with a call like SceneManager.LoadScene("path/to/options/scene.unity", LoadSceneMode.additive).
Pass input control to your other scene.
Unload the options scene when you are finished with it.
The main menu scene will have been loaded for the entire time, and you won't have to "remember" any values or use DontDestroyOnLoad.
An alternative option is to house all of your menu functionality in a single scene, and switch between multiple canvases. You can find information about that here.
you can create a class with values that you want to keep and put it on a gameobject
and use DontDestroyOnLoad(this.gameObject); so the object won't be removed when you load a new scene

When to use scene or panel

I just have a question and I don't know exactly what to type on google.
I am making a game which is always instantiating scenes now on my login process I am trying to login wrong account so there should be something that needs to pop up like an error message
Now my question is . Is there going to be a problem when I build this up because there's a tons of them or it is just normal?
But basically when i try to input the correct id and password this scenes will be deleted.
There shouldn't be a problem but this looks like an abuse of scene to me. You have too many unnecessary scenes.
Here is when you need to create or use a new scene:
1.Main Menu
This is the first scene that loads. By separating it with your game scene, you will increase loading time.
2.Game Levels
The levels in your game requires scene for each one. This makes loading them faster. You can also separate one scene into multiple ones if it's really a big scene. This also increases the speed of loading time.
You do not need scene for other things you have in your question. Those should be a UI Panels. You can create a panel by going to the GameObject ---> UI --> Panel menu. It's really easy to show/hide a panel.
For example, you have login and emergency panels:
public GameObject loginPanel;
public GameObject emergencyPanel;
to show the login panel, you disable or deactivate the emergency panel first then activate the login panel:
emergencyPanel.SetActive(false);
loginPanel.SetActive(true);
That's all you have to do.

Accessing my 'Options' panel from another script

I've created a pong clone with the following:
Main Menu (New Game, Options, About, Quit).
Level1 (Able to press ESC which opens a pause menu panel (Resume Game, Options, Quit to Main Menu).
So far I've duplicated my Options Panel from the Main Menu scene, and pasted it in the Level1 scene. Is there a better way to do it? Can I call the Main Menu Options panel from my Level1 scene? I guess creating an Options prefab would be another idea?
I would like to get it sorted before I work on my options menu (Sound ON/OFF, Sound adjustable via slider, Music ON/OFF, Music adjustable via slider) - hopefully I can implement it so it covers all scenes.
This is in C# by the way, in Unity.
You can not change values in other scene directly as the instances do not exist at that moment but you can use PlayerPrefs to save data on exiting scene and at the loading of scene you can load that data in you UI.
For example you can save and load sound volume like this:
float mySoundVolume;
void OnDestroy(){
PlayerPrefs.SetFloat("SoundVolume", mySoundVolume);
}
void Awake(){
mySoundVolume = PlayerPrefs.GetFloat("SoundVolume");
applyValuesToUI();
}
You can read more about PlayerPrefs here. https://docs.unity3d.com/ScriptReference/PlayerPrefs.GetFloat.html