So I’m making a game and I made a Store in it, and I need a script to put in a button in the store scene, that go to the previous loaded scene. For example, I completed the level one and now I’m in the level selection menu, I want to go to the store so I click in “store” button. Now, in the store, I want to go back, so I click in the “back” button and instead of going back to the level selection menu, I go to the start menu, so I need a script to put on the “back” button that brings me to the previous loaded scene. Can you help me?
put this in any class
static List<string> sceneList = new List<string>();
when you load a new scene , right before you call sceneManager
<classyouputscenelistin>.sceneList.Add("<scenename>");
when you go back
SceneManager.LoadScene(<classyouputscenelistin>.sceneList[<classyouputscenelistin.sceneList.Count -2], LoadSceneMode.Single);
replace to the class name you added scene list to
replace with the name of the scene you are loading
edit: go back to scene before last
SceneManager.LoadScene(<classyouputscenelistin>.sceneList[<classyouputscenelistin.sceneList.Count -3], LoadSceneMode.Single);
Basically Count - 2 is previous scene anything before that (-3, -4) load the scenes before it, if you don't want a scene to be tracked in this list don't do this:
<classyouputscenelistin>.sceneList.Add("<scenename>");
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 am new to unity and I am still learning. I am making a game and its going great but I have a problem with a Quit button.
It doesn't use the script attached to the On Click () section in the button menu.
There's an EventSystem in the Hierarchy, the Force Module Active is checked, the button is a child to a canvas. I don't know why the button isn't working
Here is the code in the script attached to On Click ()
public void Quit()
{
Debug.Log ("YES IT WORKS");
Application.Quit ();
}
I want the program to close when the button is pressed but when the button is pressed nothing happens!
The function is not being called when the button is pressed which means that you have not selected the function in the onClick settings. To do this, ensure that the script is attached to the button and then dragged into the onClick box of the button in the inspector. Next, click on the dropdown and hover over the name of the attached script and select the function that you want to call.
Solution
Instead of using Application.Quit() try to shift the your working scene to the home screen .Your every game should start and end at the same level which is also known as the main scene of the the game. Secondly before shifting to other scene you should destroy your player and save the scores in the player pref .
Player Pref:
The library which helps the user to store the data and show them on the main screen , Hence the use of database will not be there in order to save the data for the small games . You are beginner so you should look at these all thing on You tube or the unity website documentation available on its website
Are you in the Editor?
Application.Quit() only works when you build the application (for PC and Android. It should not work on iOS, and even if it works it should display as a crash so it's not good).
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.