Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Alexochtim · Mar 23, 2017 at 08:36 AM · scenetimerongui

LoadScene() does not reload my GUI C#

I'm new to unity and I am trying to create a Flappy Bird game for a school project. Everything is working fine except I can't find a way to reload my timer when the game is over. I'm using the following lines:

 void OnGUI()
     {
         double myTimer;
         myTimer = Math.Round(Time.fixedTime, 1);
         GUI.Box(new Rect(500, 550, 100, 20), myTimer.ToString());      
     }
 
 
     // Update is called once per frame
     void Update () {
 
         if (gameOver == true && Input.GetKeyDown(KeyCode.Space)) 
         {
             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); //startar om spelet
         }
 
     }

When I have hit something and hit space the scene reloads, however the timer doesn't. So for example if I hit something after playing for 15 seconds and hit space the game will restart but the timer will start counting in the next scene at 15 seconds. Is there an easy way to fix this in C#?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by EdwinChua · Mar 23, 2017 at 08:42 AM

Have you tried this? Also, declare mytimer outside the OnGUI() method, so that all your methods can access it.

 double mytimer;
 
 void Start()
 {
   mytimer = 0;
 }
 
 void Update()
 {
   if (gameOver == true)) 
   {
     mytimer = 0;
   }
 }


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
avatar image
0

Answer by Alexochtim · Mar 23, 2017 at 08:56 AM

@EdwinChua I copied your code but it didn't change anything, when I hit an object and restart the timer doesn't reset. Declaring the value 0 to myTimer doesn't seem to work when using it as OnGUI (). Here is my entire script as of now:

 public class GameControl : MonoBehaviour {
 
     public static GameControl instance; //gör det möjligt att anropa denna kod från andra koder genom att anropa instance.
     public GameObject gameOverText;
     public bool gameOver = false;
     public float speed = -1.5f;
     double myTimer;
 
 
     // Use this for initialization
     void Awake ()
     {
         if(instance==null)
         {
             instance = this;
         }
     }
 
     void Start()
     {
         myTimer = 0;
     }
 
     void OnGUI()
     {
         myTimer = Math.Round(Time.fixedTime, 1);
         GUI.Box(new Rect(500, 550, 100, 20), myTimer.ToString());
     }
 
 
     // Update is called once per frame
     void Update () {
 
         if(gameOver==true)
         {
             myTimer = 0;
         }
 
         if (gameOver == true && Input.GetKeyDown(KeyCode.Space)) 
         {              SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); //startar om spelet   
         }
     }
     public void BirdDead()
     {
         gameOverText.SetActive(true); //aktiverar game over texten i spelet
         gameOver = true;
     } 
 }
     

Pardon my indenting..

Comment
Add comment · Show 1 · 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
avatar image EdwinChua · Mar 23, 2017 at 09:40 AM 0
Share

@$$anonymous$$ochtim hey, I realized that you're calling Time.fixedtime. This measures the time from the start of the game.

See: https://forum.unity3d.com/threads/restart-time-fixedtime-on-level-reload.245296/

Perhaps you can use Stopwatch from Systems.Diagnostics ins$$anonymous$$d.

 using Systems.Diagnostics;
 
 class $$anonymous$$yClass
 {
  Stopwatch timer;
 
   void Start()
   {
     timer = new Stopwatch();
     timer.Start();
   }
 
   void Update()
   {
     if (gameOver == true && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) 
     { 
       
       Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex); //startar om spelet   
       timer.Reset();
       timer.Start();
     }
 
   }
 }

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

105 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

Related Questions

Post time at the end of the level 0 Answers

Countdown timer in multiple scene? 1 Answer

Save timer score from different scenes 2 Answers

How do you get a countdown timer to actually count down? (C# Script) 0 Answers

Timer Countdown Pop Up Screen Before Scene "Starts" Please Help!! 2 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