how to switch between loaded scenes - unity3d

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

Related

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

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.

Loading Scene and Main Scene

I'm building a mobile game, and my game should contain
Loading Screen with progress
Lobby screen
Game screen (with loading screen before the game loads)
No special levels in the Game...
Notice that I need to be able to share GameObjects between the scenes, for example my Network handler, which holds a connection to my servers.
How will you construct the scenes for this? What is the best practices for scenes creating?
I can thought of the following options:
Two Scenes: Loading Scene + Main Scene and share GameObjects:
The loading scene will be very lightweight scene, which will only be exists for
loading the Main Scene. It can show a progress bar according to the
progress of the main scene loading and the network login process.
Pros: The application will be loaded fast (showing the loading scene
with the progress)
Cons: I will have to share my Network handler
between the scenes (Holding the connection after a login).
Two Scenes: Loading Scene + Main Scene without sharing the
GameObjects:
I would like to be able to start loading the main scene
async, but without switching to it automatically. I want that the
main scene will start the login process for example, and only when
finished all the initialization tasks it will notify the Loading
scene that it is ready to be switched. Is this possible? Can I load
the scene in background and actually do the switching on demand?
Also I will need to be able to get the progress from the main scene
in order to show it in the loading progress bar.
Pros: No
GameObjects sharing is required - clean and isolated code.
Cons: I'm
not sure that Unity has this ability....
One scene:
Pros: All the shared GameObjects are in one place
Cons: Very slow loading time of the application (Unless there is an
option to tell unity to ignore the loading of several GameObjects
and then I could load them during the Loading screen showing time).
Thanks!
I would have 3 different scenes (one for each screen), and switching from scene to scene with the SceneManagement.LoadScene() method of UnityEngine.SceneManagement.
To share GameObjects between scenes, and more generally to share data, you have 3 options:
use a static class: good to share values
use DontDestroyOnLoad: be careful not to have duplicate objects when using that
store/load data in a file
If you want details on how to do and the differences between these 3 solutions, I advise you this tutorial : https://www.youtube.com/watch?v=WchH-JCwVI8
It is one hour long, but very useful and complete, everything is there.
I would recommend having three different scenes for the different screens.
You might have several "menu screens" in the "menu scene".
For sharing gameobjects, you can set "DontDestroyOnLoad" on the obect you want to share. From the docs:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Awake() {
DontDestroyOnLoad(transform.gameObject);
}
}
https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
For option 2:
I guess the scene won't switch to the main scene until it's finished loading. However, there might be a bit of a lag while it's loading, which might show if you're displaying a loading animation (a spinner, for instance).
It's also possible to use SceneManager.LoadSceneAsync, which will load the scene in the background. I haven't got personal experience with that one myself, but might be helpful to keep the loading screen updating while main screen is loading.
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html

How would I load a Canvas from another scene in Unity?

What I'm trying to do inside this script in Unity is to load a different scene, and find the canvas from that scene and enable it. I already know how to enable the canvas from the same scene but the issue is how can i find and enabled a canvas from another scene.
I tried SceneManager.LoadScene(scenename); to change scene so what should I add or modify? Can someone help me?
Save the canvas you want to switch to as a 'prefab' in your assets folder.
If you are loading a new scene without carrying over data from the last scene then just set up the canvas in that scene using the editor.
If you are carrying data over, or perhaps a player gameobject, then you can instantiate the canvas prefab you need. This would create it in the new scene and you can have a reference to it from wherever you chose to instatiate the prefab.
Alternatively you can have multiple canvases on the same gameobject and switch them on or off as you need them. Very useful for menus.
Hope that helps.
There're many solutions:
Using MonoBehavior's DontDestroyOnLoad(transform.gameObject) for your canvas you need to be on the next scene; You can find a lot examples in Google. Here's the link : https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Saving somewhere your current scene's gameobjects(canvas) and loading it again and recreate on the next scene as serialization etc;
Same as #2, but save required data in local variables.
There are two ways I understand your question.
You either want to copy a game object with all it's parameters as defined in the editor into another scene.
The best way to do that is obviously to use prefabs. (where to start?)
If you want to copy a game object and all its parameters after they have been modified at runtime, then you could use DontDestroyOnLoad() but that would mean the source scene has to be loaded at least once before the destination scene.

Having multiple unity scenes open simultaneously

I've been developing a board-style game in Unity3D. The main scene is the board, and has the data about each player and the current (randomly-generated) board stored within it.
I intend to add minigames into the game, for example when landing on a particular space on the board. Naturally, I would like to code the minigame in a separate scene. Is there a way I can do this without losing the instance of the current scene, so that the current scene's state is maintained?
Thanks in advance :)
Short answer: no, but there may be another way to do what you want.
A basic call to Application.LoadLevel call will destroy the current scene before loading the next one. This isn't what you want.
If your minigame is relatively simple, you could use Instantiate to bring in a prefab and spawn it far away from the rest of your scene. You can even use scripts to switch to another camera, toggle player controls and other interactions in the scene, and so on. Once the minigame is done, you can destroy or disable whatever you brought in, and re-enable whatever needs to be turned on in the main scene.
You could create a separate scene and call Application.LoadLevelAdditive to load that scene without destroying the current one. As above, you can then use scripts to manage which cameras and scene behaviors are active.
If you're careful, you don't really need two separate scenes. It may be enough to "fake" a scene switch.
Hard to give a complete answer without code, but you should look into the following things either with the unity documentation or youtube:
PlayerPrefs, this is one way of saving data, although i believe it isn't entirely secure i.e. being able to edit from a text file.
Serializable, this is apparently better than playerprefs.
DonDestroyOnLoad, can carry over information to multiple scenes.
Static variables, again not sure if this will help your particular problem.