Scene is not loading properly - c#

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?

Related

dont unload scene when switching to another scene

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.

If and ActiveSelf. Only works once. With example project

First time developer here and I am struggling with a problem.
I made a mock up of the problem in regular Unity 3d since the VR scares some talented people away from helping me. That can be downloaded here: https://gofile.io/d/qHDlUZ
File is a zip called: SoundTestTroubleShooting. A Unity build on 24.7MB
Follow the number on the buttons.
Button 1 "Choose Music" Activates the Cube.
Button 2/4 "Play Music" Checks if cube is activated and plays song if that is the case.
Button 3 "Restart Scene" Stops the music and reloads the scene.
Cube has dontdestroyonload.
After that i press button 2/4"PlayMusic" and it cant find that the cube is active.
WHY?! Going nuts here.
Old text with the entire problem here:
In my game I have four songs in the start menu that my player gets to choose from. He chooses one by pressing one of four buttons. This activates an empty GameObject, lets call it SmileMusicTrigger. SmileMusicTrigger has a dontdestroyonload and is deactivated by the other three buttons if player wants to switch song.
Player press Begin which moves him into position. He presses start and this button has:
public GameObject SmileMusicTrigger;
public GameObject EpicMusicTrigger;
public GameObject GodMusicTrigger;
public GameObject VikingMusicTrigger;
void OnTriggerEnter()
{
if (SmileMusicTrigger.activeSelf)
{
AudioManager.instance.Play("Smile");
}
if (EpicMusicTrigger.activeSelf)
{
AudioManager.instance.Play("Epic");
}
if (GodMusicTrigger.activeSelf)
{
AudioManager.instance.Play("God");
}
if (VikingMusicTrigger.activeSelf)
{
AudioManager.instance.Play("Viking");
and my game start and and the music begins fine.
When player dies he restarts the scene with restart button which has this to say about the music:
void OnTriggerEnter()
{
AudioManager.instance.Stop("Smile");
AudioManager.instance.Stop("Viking");
AudioManager.instance.Stop("God");
AudioManager.instance.Stop("Epic");
}
}
after this code has run the scene restarts and it does not seem like my Start button can check the active state of SmileMusicTrigger again. I know SmileMusicTrigger is active through the restart of the scene because I see it under dontdestroy in the hierarchy when game is running and it is checked as on in inspector. But I cant seem to call it again. Thus my music only plays once.
Audiomanager is also under Donotdestroy so I dont think the problem is there.
Sorry if this is messy. I am working really hard to get this working but I am so stuck right now.
Thankful for any help.
All the best!

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.

Unity - Load options additive to the ingame scene

at first, my most important research for this topic:
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
https://docs.unity3d.com/ScriptReference/SceneManagement.LoadSceneMode.Additive.html
When I start my game from the main menu I get into the "Ingame" scene. The ingame scene got an ingame menu with some buttons. I can click an "Options" button there.
So from the main menu I start the game by this method:
public void StartGame()
{
LoadScene("Ingame", LoadSceneMode.Single); // Load the main scene of the game
}
So when I open up my ingame menu and click on the options button I call this method
public void LoadOptions()
{
LoadScene("Options", LoadSceneMode.Additive); // Don't destroy the game and load the options menu
}
This works fine, because it doesn't destroy the ingame scene. But the problem is, that all objects are kept to the options scene. That is not, what I have expected.
How can I get into the options scene without my ingame objects and get back to the ingame scene when finishing the options?
Thanks :)
Once you are done with your Options scene, you can change your active scene back to your game and unload the menu scene. It will look something like this.
Scene gameScene = SceneManager.GetSceneByName("Ingame");
SceneManager.SetActiveScene(gameScene);
SceneManager.UnloadSceneAsync("Options");

Game over screen monogame

I am making a game using the Monogame engine and I just need help on one part of my code. Now, every time the character dies in the game, the code just ends. this is because of these lines of code.
else
{
System.Environment.Exit(0);
}
if(y < 0)
{
System.Environment.Exit(0);
}
Now I want the game to have a 'game over' screen every time a user collides with one of the obstacles and this screen will allow you to go to the menu and allow users to play again. the classes I have at the minute is the game1.cs class, the character class and the obstacle class. Thanks for all your help.
You're going to want to keep track of the game state. As an example -- you might only have two states "Playing" and "GameOver". Instead of exiting in your code snippets, set the game state from "Playing" to "GameOver".
Now in the Update call -- you'll want to check for user input during GameOver and allow them to exit the game completely or start a new game.
In the Draw call -- you'll want to check the game state to decide what to render to the screen.

Categories