Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

how to continue level ?

Discussion in 'Scripting' started by karammaks, May 17, 2014.

  1. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    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

    Code (csharp):
    1. var isQuit=false;
    2.  
    3. function OnMouseOver(){
    4. //change text color
    5. renderer.material.color=Color.red;
    6. }
    7. function OnMouseExit(){
    8. //change text color
    9. renderer.material.color=Color.blue;
    10. }
    11.  
    12. function OnMouseUp(){
    13. //is this quit
    14. if (isQuit==true) {
    15. //quit the game
    16. Application.Quit();
    17. }
    18. else {
    19. //load level
    20. Application.LoadLevel(1);
    21. }
    22. }
    23.  
    24. function Update(){
    25. //quit game if escape key is pressed
    26. if (Input.GetKey(KeyCode.Escape))
    27.  { Application.Quit();
    28. }
    29. }
     
  2. diegzumillo

    diegzumillo

    Joined:
    Jul 26, 2010
    Posts:
    418
    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.
     
  3. AlpacaMaster

    AlpacaMaster

    Joined:
    Jan 30, 2014
    Posts:
    32
    I can think of 2 solutions:

    1) 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.
     
  4. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    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 )
     
  5. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    please how can I have a menu scene in the same playing scene ???please reply :(
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Here are two ways to have the menu and gameplay in the same scene:

    1. 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.

    2. 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.