Search Unity

Resetting Score on Relaunch Of Game Scene

Discussion in 'Editor & General Support' started by adamwess, Mar 1, 2019.

  1. adamwess

    adamwess

    Joined:
    Feb 26, 2019
    Posts:
    5
    Hi all



    I have been working on creating a simple game for about a week where the main goal is to pick up as many items as possible before the timer runs out, I have managed to get the timer working and the pick-up of objects working as well as the score counter working. As well as this I have created a menu screen that when the timer runs to zero in the main game it launches you back to it, this has a start button which launches the game.



    The issue I have hit is that when you have played the game and it send you back to the menu if you launch the game again the score counter starts with the score of the last game instead of resetting to zero.



    If anyone could help it would be greatly appreciated.



    Scripts below

    ScoringSystem

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ScoringSystem : MonoBehaviour {
    7.  
    8.  
    9.     public GameObject ScoreText;
    10.     public static int theScore;
    11.  
    12.  
    13.  
    14.     void Update()
    15.     {
    16.         ScoreText.GetComponent<UnityEngine.UI.Text>().text = "SCORE: " + theScore;
    17.  
    18.     }
    19.  
    20. }
    21.  
    CollectScript

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CollectScript : MonoBehaviour {
    6.  
    7.     public AudioSource collectSound;
    8.  
    9.     private void OnTriggerEnter(Collider other)
    10.     {
    11.  
    12.         collectSound.Play();
    13.         ScoringSystem.theScore += 1;
    14.         Destroy(gameObject);
    15.        
    16.     }
    17.  
    18. }
    19.  
    20.  
    CountdownScript

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6. using System;
    7.  
    8. public class CountdownScript : MonoBehaviour
    9. {
    10.     [SerializeField] private Text uiText;
    11.     [SerializeField] private float mainTimer;
    12.  
    13.     private float timer;
    14.     private bool canCount = true;
    15.     private bool doOnce = false;
    16.  
    17.     public global::System.Single MainTimer
    18.     {
    19.         get
    20.         {
    21.             return mainTimer;
    22.         }
    23.  
    24.         set
    25.         {
    26.             mainTimer = value;
    27.         }
    28.     }
    29.  
    30.     public Text UiText
    31.     {
    32.         get
    33.         {
    34.             return uiText;
    35.         }
    36.  
    37.         set
    38.         {
    39.             uiText = value;
    40.         }
    41.     }
    42.  
    43.     void Start()
    44.     {
    45.         timer = MainTimer;
    46.     }
    47.  
    48.     void Update()
    49.     {
    50.         if (timer >= 0f && canCount)
    51.         {
    52.             timer -= Time.deltaTime;
    53.             UiText.text = timer.ToString("F");
    54.         }
    55.  
    56.         else if (timer <= 0f && !doOnce)
    57.         {
    58.             canCount = false;
    59.             doOnce = true;
    60.             UiText.text = "0";
    61.             timer = 0f;
    62.             GameOver();
    63.         }
    64.     }
    65.  
    66.  
    67.     void GameOver()
    68.     {
    69.        SceneManager.LoadScene("StartMenu");
    70.     }
    71. }
    72.  
    ButtonManager

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class ButtonManager : MonoBehaviour
    7. {
    8.     public void Start(string newGameLevel)
    9.     {
    10.  
    11.         SceneManager.LoadScene(newGameLevel);
    12.     }
    13. }
    14.  
    any help with this would be great

    thanks in advance
     
    Last edited: Mar 1, 2019
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I haven't tested, but how about:

    Code (CSharp):
    1. void GameOver()
    2.     {
    3.        ScoringSystem.theScore = 0;
    4.        SceneManager.LoadScene("StartMenu");
    5.     }
     
    unity_645AzQmecGItHw likes this.
  3. adamwess

    adamwess

    Joined:
    Feb 26, 2019
    Posts:
    5
    Thank you very much I will try this, I assume that I tag:

    ScoringSystem.theScore = 0;

    On to the end of the timer script?

    apologies as I am a novice when it comes to C#
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    No, just where I put it. You have:

    Code (CSharp):
    1. void GameOver()
    2.     {
    3.        SceneManager.LoadScene("StartMenu");
    4.     }
    and I have

    Code (CSharp):
    1. void GameOver()
    2.     {
    3.        ScoringSystem.theScore = 0;
    4.        SceneManager.LoadScene("StartMenu");
    5.     }
     
  5. adamwess

    adamwess

    Joined:
    Feb 26, 2019
    Posts:
    5
    Yeah sorry I didn’t explain myself very well that’s what I meant, from my little understanding of C# I think that this will sort the problem I’m kind of kicking myself that I couldn’t figure it out but I’ve learnt so much new stuff doing this project this week that I think my brain was just fried, I will comment back once I have tested it thanks again
     
  6. adamwess

    adamwess

    Joined:
    Feb 26, 2019
    Posts:
    5
    This works great thank you very much you are a true life saver.