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");
Related
I've been trying to switch from the main menu for our game into the first level. But the main menu keeps showing in the background no matter what I try.
So, this is our main menu (the loading screen looks just about identical, but with a loading bar in the center and without the buttons or SOMNIUM):
Yes, I know it looks ridiculous. This was a stand-in that one of our artists did in ten minutes in paint. They're still working on the real main menu art.
And this is what I get when I try to start a new game (you can see how the loading canvas from the main menu scene is still there in the background):
I got the code I'm currently using from a tutorial (I hoped copying their code would help - it didn't, but it looks nicer than mine).
public class StartGame : MonoBehaviour
{
public GameObject mainMenuCanvas;
public GameObject loadingScreenCanvas;
public Slider slider;
public void NewGame(int levelToLoad)
{
StartCoroutine(LoadAsynchronously(levelToLoad));
}
IEnumerator LoadAsynchronously(int levelToLoad)
{
// Set the loading of the level as an async operation
AsyncOperation operation = SceneManager.LoadSceneAsync(levelToLoad, LoadSceneMode.Single);
while(!operation.isDone)
{
// Get the 0-1 progress value
float progress = Mathf.Clamp01(operation.progress / 0.9f);
// Apply the progress value to the slider, which also goes from 0-1
slider.value = progress;
// Going to have a thing here to show the percentage completed as text
// End the Coroutine
yield return null;
}
}
}
Other than specifying LoadSceneMode.Single, I've trying directly destroying the main menu scene when moving to the game scene. But that had no effect. I've also trying turning off the canvas for the main menu scene when moving to the game scene. But that didn't work either.
Before loading the new scene, you can get a handle to the current scene with SceneManager.GetActiveScene. Then when the new scene has finished loading, you can manually unload the old scene with SceneManager.UnloadSceneAsync.
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!
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.
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'm makimg a game in Unity using C# developed for Android. I'm testing using Unity Remote 4. I was just wondering if a sprite can be used as a button in Unity rather than the button made in Component -> UI -> Button.
I've tried this:
private void Update()
{
if (Input.touchCount > 0)
Application.LoadLevel("startScene");
}
Which does work. The button is being used to switch from the main menu to an options menu.
Ideally, I want the button to be in the same place in the options menu to be used to go back to the main menu. The problem then is that because the scenes switch instantly, the scenes switch between each other over and over I guess because the next scene gets the touch input as well.
How can I delay the time between the scenes switching, or is there a better way to use sprites as a button??
You can create a menu system which is persistent in scenes. Have a look at This Tutorial Video for guide
Usually buttons perform their function once a tap has been performed. A tap is a press and release on the button. What you are doing is performing your function on a press which is causing your buttons to be triggered continuously while you hold your finger.
Pseudo code:
If a finger presses on the sprite
If the same finger is release on the sprite
The button has been pressed.
You can use ray casting to detect touch on the sprite
private void Update()
{
if (Input.touchCount != 1) return;
var wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
var touchPos = new Vector2(wp.x, wp.y);
if (collider2D == Physics2D.OverlapPoint(touchPos))
{
// Your code
}
}
This is one way that has worked for me:
Add the sprite that you will use as the background of the button to the stage.
Add a Collider and mark it as a trigger.
Assign a script where you add any of the following functions depending on your case.
private void OnMouseDown () {
// Runs by pressing the button
}
private void OnMouseUp () {
// Runs when button is released
}