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

.

Discussion in 'Scripting' started by NRSM, Jul 29, 2018.

  1. NRSM

    NRSM

    Joined:
    Jun 8, 2018
    Posts:
    26
    .
     
    Last edited: Feb 7, 2022
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I suspect the problem may be with your static members. If that is the case, you could try doing something like creating a reset interface. Then having a
    List<IResetStatic>
    in your
    RestartGame
    and calling Restart on them all when appropriate :
    Code (CSharp):
    1. public interface IResetStatic
    2. {
    3.     void Restart();
    4. }
    5.  
    6. public class Test : MonoBehaviour, IResetStatic
    7. {
    8.     // Note: Restart should not be called from Start() otherwise it will get reset
    9.     //  by every instantiated object.  Instead, call it explicitly when appropriate.
    10.     public void Restart( )
    11.     {
    12.         Value = <some initial value>;
    13.     }
    14.  
    15.     public int Value {
    16.         get { return m_value; }
    17.         private set { m_value = value; } }
    18.  
    19.     void Start()
    20.     {   // Not calling Restart().
    21.         ... <other code> ...
    22.     }
    23.  
    24.     static int m_value;  // Set this by calling Restart.
    25. }
     
  3. T4NK32

    T4NK32

    Joined:
    Aug 7, 2019
    Posts:
    16
    Thank you - my problem was the Time.timeScale
    - set to 0 before LoadScene - stayed 0 after reload - nothing ran : )
     
    solclovser likes this.
  4. arindamnsit

    arindamnsit

    Joined:
    Nov 15, 2019
    Posts:
    5
    Guys I learnt something today - many might be already knowing this....
    Never ever, EVER use Time or Timer functions with SceneManager.LoadScene or LoadSceneAsync.
    It completely f**ks up the GameObject movements. Timer gets slow and objects hardly move after the LoadScene call.
    Solution is to countdown frames instead of using Time or Timer functions. Like This -->


    private float ReSpawnFrameSkipInterval = 30;
    void Start () {
    level = PlayerPrefs.GetInt("Level", 0);
    }

    void Update () {
    if (ReSpawnFrameSkipInterval++ >= NextSpawnTime) {
    //Do movement stuff here.
    ReSpawnFrameSkipInterval = 0;
    NextSpawnTime = 100; // This will skip 100 frames before calling update again
    }
    }
     
    cavanas_justin likes this.
  5. An0n_Sl0th

    An0n_Sl0th

    Joined:
    Jan 21, 2021
    Posts:
    4
    Hello everyone!
    I too seem to be experiencing this problem.
    My score does not reset when the game restarts.
    I haven't a clue how to fix this as it's my first ever project, and everything I try just breaks things worse.
    For reference, here's the code I'm working.

    The Game Manager
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.     public int score;
    9.  
    10.     public bool paused;
    11.  
    12.     // instance
    13.     public static GameManager instance;
    14.  
    15.     void Awake()
    16.     {
    17.         if (instance != null && instance != this)
    18.         {
    19.             Destroy(gameObject);
    20.         }
    21.         else
    22.         {
    23.             instance = this;
    24.             DontDestroyOnLoad(gameObject);
    25.         }
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         if (Input.GetButtonDown("Cancel"))
    31.         {
    32.             TogglePauseGame();
    33.         }
    34.     }
    35.  
    36.     public void TogglePauseGame ()
    37.     {
    38.         paused = !paused;
    39.  
    40.  
    41.         if(paused)
    42.             Time.timeScale = 0.0f;
    43.         else
    44.             Time.timeScale = 1.0f;
    45.  
    46.         GameUI.instance.TogglePauseScreen(paused);
    47.     }
    48.  
    49.     public void AddScore (int scoreToGive)
    50.     {
    51.         score += scoreToGive;
    52.         GameUI.instance.UpdateScoreText();
    53.     }
    54.  
    55.     public void LevelEnd ()
    56.     {
    57.         // is this the last level?
    58.         if(SceneManager.sceneCountInBuildSettings == SceneManager.GetActiveScene().buildIndex + 1)
    59.         {
    60.             // Display Win Screen
    61.             WinGame();
    62.         }
    63.         else
    64.         {
    65.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    66.         }
    67.     }
    68.  
    69.     public void WinGame ()
    70.     {
    71.         GameUI.instance.SetEndScreen(true);
    72.         Time.timeScale = 0.0f;
    73.     }
    74.  
    75.     public void GameOver ()
    76.     {
    77.         GameUI.instance.SetEndScreen(false);
    78.         Time.timeScale = 0.0f;
    79.     }
    80. }
    And the Game UI
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using TMPro;
    6.  
    7. public class GameUI : MonoBehaviour
    8. {
    9.     public TextMeshProUGUI scoreText;
    10.  
    11.     public GameObject endScreen;
    12.     public TextMeshProUGUI endScreenHeader;
    13.     public TextMeshProUGUI endScreenScoreText;
    14.  
    15.     public GameObject pauseScreen;
    16.  
    17.     // instance
    18.     public static GameUI instance;
    19.  
    20.     void Awake()
    21.     {
    22.         instance = this;
    23.     }
    24.  
    25.     void Start()
    26.     {
    27.         UpdateScoreText();
    28.     }
    29.  
    30.     public void UpdateScoreText ()
    31.     {
    32.         scoreText.text = "Score: " + GameManager.instance.score;
    33.     }
    34.  
    35.     public void SetEndScreen (bool hasWon)
    36.     {
    37.         endScreen.SetActive(true);
    38.  
    39.         endScreenScoreText.text = $"<b>Score</b>\n{GameManager.instance.score}";
    40.  
    41.         if (hasWon)
    42.         {
    43.             endScreenHeader.text = "You Win";
    44.             endScreenHeader.color = Color.green;
    45.         }
    46.         else
    47.         {
    48.             endScreenHeader.text = "Game Over";
    49.             endScreenHeader.color = Color.red;
    50.         }
    51.     }
    52.  
    53.     // called when the "Restart" button is pressed
    54.     public void OnRestartButton ()
    55.     {
    56.         SceneManager.LoadScene(1);
    57.     }
    58.  
    59.     // called when the "Menu" button is pressed
    60.     public void OnMenuButton ()
    61.     {
    62.         if (GameManager.instance.paused)
    63.             GameManager.instance.TogglePauseGame();
    64.        
    65.         SceneManager.LoadScene(0);
    66.     }
    67.  
    68.     // called when the game is paused or unpaused
    69.     public void TogglePauseScreen (bool paused)
    70.     {
    71.         pauseScreen.SetActive(paused);
    72.     }
    73.  
    74.     // called when the "Resume" button is pressed
    75.     public void OnResumeButton ()
    76.     {
    77.         GameManager.instance.TogglePauseGame();
    78.     }
    79. }
    80.