Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved When i restart the score does not restart

Discussion in 'Scripting' started by Jinngineer, Oct 4, 2020.

  1. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    Hello,

    As is the title,i am having an issue with restarting the score when the game is restarted.

    I am restarting by using a button in either a game over menu or from the main menu.

    Apologies for my messy code :)

    Game Manager Script
    Code (csharp):
    1.  
    2. public class GameManager : MonoBehaviour
    3. {
    4.     public GameObject[] obstacles;
    5.     public GameObject spawnLocation;
    6.     public bool gameIsActive = true;
    7.     public GameObject timer;
    8.  
    9.     Text timeText;
    10.     Vector3 spawnPosition;
    11.     int randObstacle;
    12.     float randSpawnTime;
    13.  
    14.     public float startTime;
    15.     private GameOverScript gameOverScript;
    16.  
    17.    void Start()
    18.     {
    19.         timeText = GameObject.Find("Timer Text").GetComponent<Text>();
    20.         startTime = Time.time;
    21.  
    22.         gameOverScript= GameObject.Find("Canvas").GetComponent<GameOverScript>();
    23.  
    24.         StartCoroutine(SpawnObstacle());
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         if (gameIsActive)
    30.         {
    31.             TimeTextFuction();
    32.             timer.SetActive(true);
    33.         }
    34.      
    35.     }
    36.  
    37.      public string TimeTextFuction()
    38.     {    
    39.         startTime += Time.deltaTime;
    40.         string minute = Mathf.Floor((startTime % 3600) / 60).ToString("00");
    41.         string seconds = (startTime % 60).ToString("00");
    42.         timeText.text = minute + ":" + seconds;    
    43.         return timeText.text;
    44.     }
    45.  
    46.     private IEnumerator SpawnObstacle()
    47.     {
    48.         while (gameIsActive)
    49.         {
    50.             spawnPosition = new Vector3(obstacles[SpawnRandObstacle()].transform.position.x, spawnLocation.transform.position.y, spawnLocation.transform.position.z);
    51.             Instantiate(obstacles[SpawnRandObstacle()], spawnPosition, spawnLocation.transform.rotation);
    52.             yield return new WaitForSeconds(WaitRandomSecond());
    53.         }
    54.     }
    55.  
    56.    int SpawnRandObstacle()
    57.     {
    58.         randObstacle = Random.Range(0, obstacles.Length);
    59.         return randObstacle;
    60.     }
    61.  
    62.     float WaitRandomSecond()
    63.     {
    64.         randSpawnTime = Random.Range(0.6f, 0.8f);
    65.         return randSpawnTime;
    66.     }
    67.  
    68.     public void GameOver()
    69.     {
    70.         gameOverScript.GameOverPanelActive();
    71.         gameOverScript.gameOverText.text = "Game Over " + "\r\n" + "Your Time: " + timeText.text;
    72.         timer.SetActive(false);
    73.         gameIsActive = false;
    74.     }
    75. }
    76.  
    Main Menu Script

    Code (csharp):
    1.  
    2. public class MainMenuScript : MonoBehaviour
    3. {
    4.     private GameManager gameManager;
    5.  
    6.     private void Start()
    7.     {
    8.         gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
    9.     }
    10.  
    11.     public void Play()
    12.     {
    13.         SceneManager.LoadScene(0);
    14.      
    15.     }
    16.     public void Quit()
    17.     {
    18.         Debug.Log("Quit Game");
    19.         Application.Quit();
    20.     }
    21.     public void Options()
    22.     {
    23.         Debug.Log("Options Menu");
    24.      
    25.     }
    26. }
    27.  
    28.  
    Game Over Menu Script

    Code (csharp):
    1.  
    2. public class GameOverScript : MonoBehaviour
    3. {
    4.     public GameObject gameOverPanel;
    5.     public Text gameOverText;
    6.  
    7.     private GameManager gameManager;
    8.  
    9.     void Start()
    10.     {
    11.         gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
    12.         gameOverText = gameOverText.GetComponent<Text>();
    13.     }
    14.  
    15.  
    16.     void Update()
    17.     {
    18.         if (!gameManager.gameIsActive)
    19.         {
    20.             GameOverPanelActive();
    21.         }
    22.     }
    23.  
    24.     public void GameOverPanelActive()
    25.     {
    26.         gameOverPanel.SetActive(true);
    27.     }
    28.  
    29.     public void Restart()
    30.     {
    31.         Debug.Log("Restart");
    32.         SceneManager.LoadScene(0);
    33.      
    34.     }
    35.  
    36.     public void MainMenu()
    37.     {
    38.         Debug.Log("Return to Main Menu");
    39.         SceneManager.LoadScene(1);
    40.     }
    41.  
    42.     public void Quit()
    43.     {
    44.         Debug.Log("Quit Game");
    45.         Application.Quit();
    46.     }
    47. }
    48.  
    Any and all information would be appreciated.

    Best Regards :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    PraetorBlue likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
  4. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    Ummm, are there new code tags? cause the old ones are not working.... Tried both <CODE></CODE>
    amd <code></code>
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    Square brackets!

    Code (csharp):
    1.  foo
     
    PraetorBlue likes this.
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    Square brackets my friend.
     
    Kurt-Dekker likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    Yeeeeet ,man, you and I are at it again baby. :)
     
    PraetorBlue likes this.
  8. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    Whoops xD. Apologies, everything has been edited
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
  10. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    @PraetorBlue and @Kurt-Dekker ... Are yall always on here?? i feel like whenever i got an issue you are the first to reply
     
    Kurt-Dekker likes this.
  11. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    I'm on more than is healthy. It's my way of procrastinating from doing my own work :(
     
    Kurt-Dekker likes this.
  12. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
  13. Jinngineer

    Jinngineer

    Joined:
    Apr 22, 2020
    Posts:
    41
    :(

    It worked :D... now just to read the Unity API so that i can understand why xD
     
  14. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    The answer is just in the first sentence of each of those pages. Time.time tells you the time since the entire game started. So if you died and reloaded the scene, it also includes your previous run, and the one before that, and any time you've spent in menus etc...

    Time.timeSinceLevelLoaded is just the time the player has been in the current scene, which seems more like what your game is trying to do. You could probably also just use
    0
    and get the same effect, since this happens at the very start of the scene.