- Home /
How to fix Score bug when I Restart the game?
I am trying to make a simple game to test Highest Score System. I have two Scenes Start and LeveL1. On The Start Scene I have One button "Start Game", and a text "Highest Score: 0". When i click Start Game i go to the next Scene, On The Level1 Scene i Have Text "Score: 0", and one button "Main Menu". I just want to make a simple game where i click start game and it increases the score according to time(Time.deltatime), So each Second i increment Score by 1. Everything Works Great, However when I click Main Menu and click on Start Game (Again) I want to start my game from score 0, however the game automatically starts from the Highest Score, i think there is something wrong with deltatime. On The Start Scene i Have one object ScoresInfo where my script of highest score calculation is Attached, i think that because this object is not being destroyed(because i made it singleton, so that i would transform the points information from one script to another), the Time.delta time is not being restarted and the game starts counting from highest score and not from 0. How can i fix this problem? is there any solution to restart Time.deltatime when I click start button again? Here is my script also:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class HScores : MonoBehaviour
{
public int Score;
public int HighestScore;
public int Increment = 1;
float ScoreFloat;
private bool ResetScoreOnStart = false;
public void Update()
{
AddScore();
CheckHighScore();
ResetScore();
}
public void AddScore()
{
int index = SceneManager.GetActiveScene().buildIndex;
if (index==1)
{
ScoreFloat = ScoreFloat + Increment*Time.deltaTime;
Score = (int)ScoreFloat;
}
}
public void ResetScore()
{
int index = SceneManager.GetActiveScene().buildIndex;
if (index==0)
{
Score = 0;
ResetScoreOnStart = false;
}
if (index == 1)
{
if (ResetScoreOnStart==false)
{
Score = 0;
}
ResetScoreOnStart = true;
}
}
public void CheckHighScore()
{
if (Score>HighestScore)
{
HighestScore = Score;
}
}
void Awake()
{
int number = FindObjectsOfType<HScores>().Length;
if (number > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
}
Answer by givexa · Mar 17, 2021 at 08:05 PM
I have Fixed the issue, with dividing my code into two parts, and also dividing my gameobject "ScoresInfo" into two parts. First gameobject "ScoreInfo" only had the information of Highest Score, which was a singleton, the next GameObject I Created was "ScoreCalculator", which I added in the next Scene, which was increasing score with time.deltatime, These two Scripts were connected with each other with GameObject temp = GameObject.Find("ScoreCalculator"); HScoreIncrease ScoreIncrease = temp.GetComponent<HScoreIncrease>(); if (ScoreIncrease.Score > HighestScore) { HighestScore = ScoreIncrease.Score; }
So the issue was, Increasing score function should not have been a singleton, and Highest Score should have been, so i had to divide the code in to two gameobjects. I Would Really want to hear other solutions if there are any.
Your answer
