Accessing my 'Options' panel from another script - unity3d

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

Related

In unity how to make in-game menu?

I'm making a Unity2D mobile game but I'm stuck on making a menu. I want the in-game menu and if the player touches the screen or clicks the screen, the game will start and the menu will be have store, options but I don't know how to do it. I tried to do the same scene but this time when it goes to the next level, the menu disappears. I searched on the internet but couldn't find it. Thanks.
You should first create a UIManager script for yourself.
In this script, you must define 2D elements such as Text, Canvas, Panel and provide their functionality.
Then you can control these functions in your main manager (GameManager).
To put it simply, it takes almost a video tutorial to explain it.
Because this is not a simple code error question, man.

Multiple scenes playing at once in Unity

So the problem is i've just added a simple menu system to my game and it seems to load both scenes at the same time. The game is running in the background which is annoying, but somehow after pressing play it resets the game scene and starts playing the game normally. In the build settings the gamescene is named pretty weirdly, but every time i drag my game scene from the hierarchy, it gives me that name in the build settings. Any tips on how to make the scenes work properly?
public class MainMenuN : MonoBehaviour
{
public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void QuitGame (){
Debug.Log("QUIT");
Application.Quit();
}
}
Well, you have currently both scenes loaded in the Hierarchy!
So when pressing play to enter the playmode both scenes are loaded and running.
Then after pressing the button your do SceneManager.LoadScene without passing in the mode parameter, so it uses the default mode which is Single.
Closes all current loaded Scenes and loads a Scene.
That's why it works after that since this makes all currently loaded scenes be unloaded and the target scene loaded.
This Multi Scene Editing is a pure editor feature and will not behave like that in a build where only the very first scene from the settings will be loaded.
Especially be careful with cross scene references!
For testing correctly in the editor and replicate the same behavior your app will have after building save and then remove the second scene (Ropesgame) from the Hierarchy.

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.

Unity - create a common menu for multiple scenes

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.