I have a audio player during scene 0 in my game (the menu). I have a code that intends to prevent the objects destruction when changing scenes. This works.. mostly. But for some reason, when moving to scene 1 a second audio player is created. This does not happen when moving to scene 2, 3, etc. I cannot find any components in scene 1 indicating a second audio player.
I have attached screen shots that may be of assistance. Any help with this would be greatly appreciated. I am stumped.
audio player and code ....
duplicated audio in scene 1....
In your Scene Changing Script, Create a New Function (or edit existing function)
LoadSceneWithoutAudioDestruction or Whatever seems good to you
Then Write down the Following Code.
public GameObject AudioSouce; //in starting of file to reference Audio
public void LoadSceneWithoutAudioDestruction(){
DontDestroyOnLoad(AudioSouce);
SceneManager.LoadScene(SceneNameasString);
}
Now on button which you want to Load next Scene attach this Script and ad function LoadSceneWithoutAudioDestruction to On Button click Event of that button.
Also Ensure that the next scene that you are Loading does not habe any Audio Source Already
If any Problem exists, ask me in comment.
Related
I have a game where I need to save a scene. I'm doing this by setting the scene to a variable which I can access later. The player then goes to a main menu when they are done.
The player now wants to go back to that scene, however, when it loads normally, it resets to its original state. Is there any way to use this variable to set the scene's contents to the variable I made earlier?
This is how I made the variable: Builder1 = SceneManager.GetActiveScene();
Any help would be greatly appreciated (and yes, I do realize that the variable I made probably doesn't store the scene the way I want it to). Thanks!
Scene is just a reference to the Scene object, and not all of the GameObject's locations/information at that moment in time. If the information in a scene shouldn't be lost yet, then it is advisable to not close the scene yet. Instead, take a look at Additive Scene Loading. You can addivitely load your new scene, switch your main camera to the new scene's camera, and when you are done there you can close the added scene and return the camera to the original scene.
If you're looking to persist data between game loads, then you'll need to consider serializing all of the relevant data in your scene and build a data class to house all of it.
I have a scene that I'm working on using Steam VR 2.0, and Unity 2018.3.2f1. I have a simple statement in it that reloads the scene
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadSceneAsync("Final");
}
}
The issue is: when I reload the scene, it stops responding properly. I am still able to move my head around, and hover over objects. And the objects I hover over get highlighted, but they stay highlighted. I'm not able to pick them up, or interact with them in any meaningful way, and I don't know why this is happening.
I've attached a screenshot of the issue below.
As you can see, multiple objects are highlighted, and the hand mesh is weird:
Solutions I've tried--
Using LoadScene instead of LoadSceneAsync
Using Application.LoadScene instead
Tried to edit the Player script in SteamVR library to not add it to Don't Destroy On Load
Any suggestions?
The issue was arising because the Player prefab in SteamVR 2.0 had Do Not Destroy on Load checked. So, there were multiple players being instanced when I reloaded the scene. I unchecked that box, and now everything is in order.
The checkbox is located inside the [SteamVR] object under the Player prefab:
I have made block breaker game in unity 4.6 and i have added 3 levels. If anyone loses in any level say level_02 then the lose screen showing game over loads up. When he clicks try again it loads the start screen as i have ordered them in my build settings. I want my game to load the same level where the user have lost so that he clicks try again and the same level loads again i.e level_02.
You can save the current levelScene name before you loads the looseScene like,
PlayerPrefs.SetString("LastLevelName",ScaneManager.GetActiveScene().name);
and when you click the restart button in looseScene then call some method like this,
public void RestartSameLevel(){
SceneManager.LoadScene(PlayerPrefs.GetString("LastLevelName","DefaultSceneName");
//"DefaultSceneName" can be your StartScene name
}
You can restart the scene
Application.LoadLevel(Application.loadedLevel)
Or
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
I have two scenes: Menu and Game. I'm using C#.
When you die in the game, you get sent back to the menu with
SceneManager.LoadScene("Menu");
There's a script with a GUIButton in the menu that, when clicked, loads the game with
SceneManager.LoadScene("Game");
What I want to happen is that when I click the button in the menu scene, it loads the game as if I just clicked the play button with the game scene open in the editor.
What instead happens is that it goes to the game scene, but some objects from the game scene appear to be missing. I'm not using DontDestroyOnLoad() anywhere.
Some objects from the scene do appear, but others don't. The weirdest thing is that there are some data fields on a script on the missing object that are referred to by some other scripts, and those give values that make sense.
Does anyone have any idea what's going on, or what I can do to get the desired result?
Turns out that the problem was that I was destroying one of my objects if it was not the first time that object was created, and I didn't realize it.
are those static objects or dynamic? try to instantiate them in function which gets called on scene loaded.
I have a level selection menu, inside this I have buttons to load my scene levels.
I am using the next method to switch the scenes:
UnityEngine.SceneManagement.SceneManager.LoadScene();
The problem is that I have dark-gray faders in my scene and when I load the different scenes the screen flashes white. Instead of staying in the same dark-gray tone like I would want it.
I will gladly supply any further information that you need.
Does anyone know a solution?
The last frame of the previous scene is contiguous with the first frame of the next scene so one needs to ensure that there is nothing in either frame that could make a visible difference e.g. resetting the state of something like a material colour via a script called from an Awake function.