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.
Related
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.
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*);
I'm newbie to Unity 5 and I have problem with creating new scenes. After creating a project ,I go to File->New Scene but the scene does not appear at all so I can't do anything with it ! Here's the image from what I see in Unity:
Your new scene is there. Your layout is likely messed up.
Click on the Default in the image above(where red arrow points to) then click Default. It should reset everything to normal. You can press Control + S to actually save the scene.
Hit Layout in the top right corner, select "2 by 3"
You will see your scene and game view quickly that way.
Have you enabled the scene view and switched to it?
Maybe you are viewing the game view. There is nothing in the scene so nothing will be displayed in game view.
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?
I have a script called StartGame that is suposed to do different things when different game modes are selected. Unity's API and forums suggests using OnLevelWasLoaded, but it isnt being called. In fact when I hover my mouse over it in Visual Studio it tells me the method is"StartGame.OnLevelWasLoaded". I'm pretty sure that means that It isn't overriding the method. Has anyone else had this problem? Also I'm using Unity 5.3.
Are you sure your GameObject is still existing after loading the new scene?
Usually, all GameObjects are deleted from the scene when loading another scene.
You can prevent this by loading the scene additive (adding the contents of the new scene to the contents of the current one)
SceneManager.LoadScene("your scene name", LoadSceneMode.Additive);
or by preventing deletion for your specific GameObject
GameObject.DontDestroyOnLoad(yourStartGameScriptHolder); // where yourStartGameScriptHolder is the GameObject(!), not the script reference