dont unload scene when switching to another scene - c#

i have 2 scene in my program, when click a button go to the second scene and when press home button (in the second scene) go to the previous scene, how can keep loaded the first scene? I tried the Scene Manager additive mode but it shows both scenes together, how can i do this?

If you want to keep the first scene loaded but hidden when the second scene is shown, it is not possible. But what you might want to try is to use SceneManager.LoadSceneAsync when you call the first scene from the second scene

I don't know the code but i am pretty sure you can find all the children of the scene make them setActive(false) and make everything hidden.
You need to use Scene Manager additive mode ofcourse.

Related

Is there a way of loading up a scene when my player collides with a door?

I'm trying to make it so when my player gets five points and they collide with the door, the menu screen for my game shows up. I thought this code was fine but the main menu won't show when I collide with the door. Can anyone help me fix this? Here is my code below:
private void OnTriggerEnter(Collider door)
{
if(door.tag == "Door" && points == 5)
{
SceneManager.LoadScene("MainMenu");
}
}
}
Im also using unityengine.scenemanagement too. The rest of the code including the points works fine it's just the loading of the scene that won't work.
Some scenes have not been loaded into the build settings, so when jumping from the current scene to other scenes, other scenes cannot be loaded. You must load all the scenes you want to use into the scenes in buding list in the build settings. Method: After loading a scene, click the build settings option of the FILE menu, open the build settings menu, and then click the "add current" button in the menu. You can see that the current scenes are added to the build settings menu list. Open other scenes in this way and add them to the scenes in buding list in the uild settings menu. After adding all the scenes, save the project and run it again, I hope it can help you
There are four things happening here, so you should check all of them in order to find your problem.
Put a Debug.Log() in OnTriggerEnter to make sure that it's actually being called
Print out door.tag to make sure that it matches "Door." Note that it's case sensitive.
Print out points to make sure that the value matches 5.
Double check that your scene name matches "MainMenu." (EDIT: not case sensitive) You can also load the scene by build index so that there's no risk of mismatching magic strings.
SceneManager.LoadScene (/* your scene index */);
Or
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex + 1);
Scene indexes begin at 0 and count up according to the order the scenes are arranged in your build settings. That can be found in File > Build Settings

Can't change to the specific scenes using UI buttons in unity, why?

So, to start, I have this code:
public void switcher(string scene_name)
{
SceneManager.LoadScene(scene_name, LoadSceneMode.Single);
}
That's supposed to load a scene given in the editor, and I have 2 buttons, each supposed to load a specific scene and 2 gameobjects (one for scene) that have the code written before.
Object with script
Button that calls object
Now, the thing is, despite having different scene names, both buttons load the same scene, and when I do some changes (mainly deleting and re-adding components) then both buttons load another scene, but never one scene each.
Why is this happening?
First of all to load your scene you must add it to the build settings.Go to File>Build settings and then drag your scene into the Scenes in build.Sencond thing you must add this line of code to set you scene to be the active one
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + *Add you scene number here*);

Trying to totally reload scene

I made a scene that represents the main menu of a game. In this menu there is an option for single player, and, when I press it, it opens a new "level 1" scene.
Also, there is a pause menu that allows the player to quit the game and get back to the main menu.
However, after it backs to the main menu, when I try to get into the level 1 scene using SceneManager.LoadScene("level 1"), it makes a lot of problems. It means that a lot of game objects don't act like they should.
I dont know why it happened, and I would appreciate if someone can help me solve it. Thank you.
You can use SceneManager.LoadScene("level 1", LoadSceneMode.Single); to clear the old scene and only load the new one in, which will let you get a clear scene and shouldn't have any problems.
If you have scripts that need to last into the new scene, you can use DontDestroyOnLoad(this); in your scripts and they will move into the new scene. The problem is you have to make sure your scripts that aren't being destroyed don't try and access destroyed objects. From your comment it looks like you have a player movement controller that isn't being destroyed, or it's being created in the menu scene and it doesn't have it's references updated correctly when the scene is reloaded. You can also use Destroy(this); in your scripts that are saved through scene changes to get rid of them when they're done running.

Scene is not loading properly

I have 2 scenes in my game. 1st for menu and 2nd for Game itself. There is one button on screen called "Play". This button will let player load Game Scene. And if user press Escape from Game Scene, it will load Main Menu. I've uploaded code for it below,
if (Input.GetKeyDown (KeyCode.Escape)) {
SceneManager.LoadScene ("Menu");
}
The above code(Written in Update()) exits the Game Scene, But The player remains on screen. And so if I again press Play, the Game Scene does not display player. I've searched for it much, but I can't find any understandable reason. Can anyone help me please.
The problem could be that you have called DontDestroyOnLoad(player) somewhere.
Have you tried making a different scene and loading that one to test if that also happens?

Animation Playing While Loading Scene One to Other in C# Unity3D?

I need to know about Animation Playing While Loading Scene One to Other in C# Unity3D.
I am using the below code to Navigation. It navigates to Scene2, but it doesn't looks very good.
Application.LoadLevel ("Scene2");
How can I apply a loading animation while the level loads?
From http://answers.unity3d.com/questions/39317/animated-loading-screen.html:
Create a scene that will be your loading screen, do what you want to do with this scene (an animation or whatever you want). Don't forget to make this small to load.
Create an object with a script and in the Update function of this script just put these lines:
if(Application.GetStreamProgressForLevel("Scene2") ==1){
Application.LoadLevel("Scene2");
}
Make sure that you put these scenes in order when publishing:
LoadScreen
Scene1
LoadScreen
Scene2
The Application.GetStreamProgressForLevel() function returns a float number between 0 and 1, you can use this to make a progress bar too.
Additional reading:
http://forum.unity3d.com/threads/loading-screen.38405/
http://forum.unity3d.com/threads/adding-loading-screen-while-unity-is-getting-loaded.17298/
Create new scene (loading) Coding you're loading screen
Or use this resource :https://www.assetstore.unity3d.com/en/#!/content/6354
then after you create the loading scene
use this code (scene (loading)) add the code after the level finish
Application.LoadLevel("loading");
and on loading scene change the name of level use this to navigate you to next level
Application.LoadLevel("level2");

Categories