Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by givexa · Mar 17, 2021 at 06:07 PM · unity 2dtime.deltatimehighscores

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);
         }
     }
 }
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UnityWebRequest - Going to a URL (without opening) 1 Answer

Only able to Hit the Z key once every 10 seconds 1 Answer

gameobject not setting active after timer ends 2 Answers

Using the increasing and decreasing value function to deal damage 2 Answers

How to make game object move every 10 seconds? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges