Multiple scenes playing at once in Unity - unity3d

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.

Related

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.

unity, LoadSceneMode.Additive mode not working in window build

I want to show TWO Scene simultaneously in my application.
EDITOR has not any problem. working good. but if I build for window
application show just one scene, how to fix it?
When you build the project, by default, only the first scene on your build order will be loaded as the first displayed scene. If you want to load multiple scene simultaneously, you should load the extra scenes additively on a script, for example in C#:
SceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive);
However in the editor, as you noticed, you could add two scenes and both loaded and runs normally when you hit "Play". This is just one of the editor's helpful functionality for editing multiple scene. Notice that on project hierarchy, only a single scene is recognized as the "Active scene". The editor behaves as if you loaded the active scene in "Single" mode first, then loaded the others in "Additive" mode.
Ref: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

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

Why the game view gets dark when play again button is clicked for replaying a scene in Unity 5.2?

when I reload a game scene by clicking on play again button in Unity , the game view automatically gets dark as if the skybox lighting settings are not set at all.
The game scene plays fine for the first time , the file was saved and when trying to replay the same again,the light settings go off. Is it a lighting bug in Unity 5 version?
I had the same problem and fixed it by unticking "Auto" (for automatic baking of light maps) in Window>Lighting>Scene and clicking "Build".
The same problem is answered on the Unity website
I also had the same issue. Turn off Baked Global Illumination in Mixed Lighting section.
Check if you are Destroy()ing objects you are not supposed to destroy.
You might for example have this case:
public class LightInstantiator : MonoBehaviour
{
public GameObject lightPrefab;
private GameObject mySceneLight;
void Start()
{
mySceneLight = Instantiate(lightPrefab);
}
void OnDestroy()
{
DestroyImmediate(lightPrefab); // BUG: you destroy the Prefab instead of the instance
}
}
This example script instantiates the Scene light for me, and destroys it again when itself is destroyed. This is perfectly legit, but I accidentally have introduced a bug: where I meant to destroy the instantiated light (mySceneLight) I actually destroy the Prefab.
On first start everything seems fine, light and all. When stopping and restarting the scene however, Unity will feed the (previously destroyed) Prefab into my script, which will then be unable to instantiate a functioning light: the scene stays dark.
Something along these lines happened to me once. I don't know if this is still possible, but it might be worth a check.
Afterthought:
It may even be easier. You might accidentally modify the prefab light in code by setting its intensity to 0 during your first run. As it is a prefab, Unity might not reset that, and on your second run, the prefab light intensity is still 0.

How to Send message to another scene

I have two scenes in my game(Main Menu and Game).
There are two buttons in Main Menu each sending different values to Game.
So how can i set this value in other scenes script.
If I understand correctly you are trying to pass settings between the main menu scene and the game scene.
What you can do is create a GameSettings script that contains all your settings, and stick it on a game object of your main menu scene.
Store your settings in this script.
Loading the game scene would normally destroys all existing game objects, but Unity provides the Object.DontDestroyOnLoad() function that will prevent an object from being destroyed at loading.
Use this on your GameSettings script and it will exist in both the main menu and game scene and you will be able to retrieve your stored settings from the game scene.
Check the documentation for more info: http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Here is an example:
public class GameSettings : MonoBehaviour
{
public int NumberOfPlayer;
/* Add other game settings here ... */
void Awake()
{
DontDestroyOnLoad(transform.gameObject)
}
}
Hope it helps :)
Any variable declared static in a script will be preserved across scene changes. If you have a script in both scenes, and set the value of a static variable in one of the scenes, the script instance in the other scene will access the same value.