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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Something is wrong with my game.

Discussion in 'Scripting' started by andro321111, Nov 22, 2015.

  1. andro321111

    andro321111

    Joined:
    Nov 20, 2015
    Posts:
    29
    Hi!

    I made a game and i created main menu and pause menu. So when i click new game i start lvl 1 and i come to finish and then lvl 2 load and all work perfect and then in lvl 2 i open pause menu and click on go back on main menu and this work perfect too but then when i click again new game lvl 1 load but my time stay same as for previus "new game" and when i want to enter level 2 its says cannot load lvl 3... but if its new game it should load lvl 2.. something is wrong and idk what.. Please help.

    Pause menu script:
    Code (CSharp):
    1. using System.Linq;
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6.  
    7. public class PM : MonoBehaviour
    8. {
    9.  
    10.     #region Attributs
    11.  
    12.     private bool isPaused = false;
    13.  
    14.     #endregion
    15.  
    16.     #region Proprietes
    17.     #endregion
    18.  
    19.     #region Constructeur
    20.     #endregion
    21.  
    22.     #region Methodes
    23.  
    24.     void Start ()
    25.     {
    26.      
    27.     }
    28.  
    29.  
    30.     void Update ()
    31.     {
    32.  
    33.         {
    34.             Cursor.visible = true;
    35.         }
    36.  
    37.         if(Input.GetKeyDown(KeyCode.Escape))
    38.             isPaused = !isPaused;
    39.      
    40.      
    41.         if(isPaused)
    42.             Time.timeScale = 0f;
    43.      
    44.         else
    45.             Time.timeScale = 1.0f;
    46.      
    47.      
    48.     }
    49.  
    50.     void OnGUI ()
    51.     {
    52.         if(isPaused)
    53.         {
    54.          
    55.  
    56.             if(GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height / 2 - 60, 180, 50), "Continue"))
    57.             {
    58.                 isPaused = false;
    59.             }
    60.  
    61.  
    62.             if(GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height / 2 + 00, 180, 50), "Main Menu"))
    63.             {
    64.                 Application.LoadLevel("Main_Menu");
    65.             }
    66.  
    67.             if(GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height / 2 + 60, 180, 50), "Quit Game"))
    68.             {
    69.                 Application.Quit();
    70.  
    71.             }
    72.          
    73.         }
    74.     }
    75.  
    76.     #endregion
    77. }
    Timer code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Timer : MonoBehaviour
    6. {
    7.  
    8.     public Text counterText;
    9.  
    10.     public float seconds, minutes;
    11.  
    12.     void Start ()
    13.     {
    14.         counterText = GetComponent<Text> () as Text;
    15.     }
    16.  
    17.     void Update(){
    18.         minutes = (int)(Time.time/60f);
    19.         seconds = (int)(Time.time % 60f);
    20.         counterText.text = minutes.ToString ("00") + ":" + seconds.ToString ("00");
    21.     }
    22. }
    So how can i make that time and level will be destroyed when i click main menu button.. i dont want them to be saved and ruin game.
     
  2. coward

    coward

    Joined:
    Jan 9, 2013
    Posts:
    21
    Application.LoadLevel will wipe out everything from the scene you are currently in. You could have a game manager class/monobehaviour that you mark up with [DontDestroyOnLoad] which will ensure it is not destroyed across levels, and that could track the current level (so when the player clicks on new game they go to the correct level). It could also have the timer on it, or you can have the timer marked up the same way. If you want to store it between opening and closing of your game, you could look into PlayerPrefs to save the values.
     
  3. andro321111

    andro321111

    Joined:
    Nov 20, 2015
    Posts:
    29
    i have manager script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Manager : MonoBehaviour {
    5.     public static int currentLevel = 1;
    6.     public static void CompleteLevel()
    7.  
    8.     {
    9.         Application.LoadLevel (currentLevel+1);
    10.         currentLevel += 1;
    11.     }
    12. }
    can you try give me example please? im new at making games and need to learn alot.
     
  4. andro321111

    andro321111

    Joined:
    Nov 20, 2015
    Posts:
    29
    i wanted to do something with pause menu script.. i was thinking like if i press this button "Main Menu" time and level will be deleted and load level main menu. but it doesnt work :) was i close i just want restart everything when i go to main menu..

    Code (CSharp):
    1. if(GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height / 2 + 00, 180, 50), "Main Menu"))
    2.             {
    3.                
    4.                 Destroy (GameObject.FindWithTag("Manager"));
    5.                 Destroy (GameObject.FindWithTag("Timer"));
    6.                 Application.LoadLevel("Main_Menu");
    7.             }
    8.  
     
  5. andro321111

    andro321111

    Joined:
    Nov 20, 2015
    Posts:
    29
    still need help...
     
  6. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    I'm thinking wouldn't it be easier to save all the stats in PlayerPrefs? Then for each level you save your best time/score.

    Now the tricky part would be splitting your time/score into different sections eg, levelOneBestTime, levelOneCurrentTime etc etc.

    Then you can easily save you time with playerprefs ec if currentTime < bestTime save etc..

    Doing this you can then on a new game add a PlayerPrefs.DeleteAll erasing the data and therefor starting a fresh new game.

    Otherwise I'm not all that sure what else to try this is the first thing that came to mind.