I have a code for loading main menu , but when load the game level again , it start the level from beginning , I wanted to continue playing the playing level not to play from begin
var isQuit=false;
function OnMouseOver(){
//change text color
renderer.material.color=Color.red;
}
function OnMouseExit(){
//change text color
renderer.material.color=Color.blue;
}
function OnMouseUp(){
//is this quit
if (isQuit==true) {
//quit the game
Application.Quit();
}
else {
//load level
Application.LoadLevel(1);
}
}
function Update(){
//quit game if escape key is pressed
if (Input.GetKey(KeyCode.Escape))
{ Application.Quit();
}
}
How is this being called? Is this in a special scene and you’re loading it while you were playing in a different scene? If so returning to the previous scene would require some fancy saving and loading feature to be coded. If, on the other hand, this script is in the same scene then… it depends on how you’re calling it, really. My game menu worked like this: the player press esc or start and the game pauses (I set time scale to 0) then the whole menu loads inside a OnGUI function (there’s a script and a gameobject dedicated to this). So if I press start or esc again I just leave this state entirely, hide all this gui stuff and restore time scale.
You can have the menu on the same scene and when you go to the menu use Time.TimeScale = 0 to pause the game edit: diegzumillo said that before me:)
2)You can use PlayerPrefs to save data like where is your player, where are other objects , etc… for more information about PlayerPrefs go to the scripting manual.
yes I am making 2 scene , the playing scene and menu scene , when I press escape , I loads the menu scene after this I want a code to resume the playing scene from last position , ( I just want a continue code or function to load the previous scene from last position )
Here are two ways to have the menu and gameplay in the same scene:
Draw the menu on top of the gameplay world stuff. If you use OnGUI, it always draws on top of the world. Since the menu pauses the game, the world will stop until you resume the game.
If your menu is made of GameObjects with colliders, put all of your menu objects under a single parent object. Put all of your gameplay objects under a different parent. When you open the menu, activate the menu parent object and deactivate the gameplay parent object. When you close the menu, deactivate the menu parent object and activate the gameplay parent object.
If you switch scenes, you have to do like diegzumillo and AlpacaMaster wrote. Save the state of the gameplay scene before changing to the menu scene. When you return to the gameplay scene, restore the state. Here are some ways to save the state:
Record the state in PlayerPrefs.
Record the state in a static variable or singleton object that doesn’t get destroyed between level changes.
Use a product like Easy Save 2 to serialize the level.