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 /
avatar image
0
Question by ethranes · Jul 24, 2016 at 08:44 AM · c#timescene-switchingdontdestroyonloadcountdown

countdown to persist through scenes

Hi,

I have a countdown clock in my game which I want to run through multiple scenes while keeping the time from the previous scenes. I have a timer coded, and I have a DontDestroyOnLoad added.

I've tried putting the code into each scene but it will restart the countdown, I have also tried only adding the code to the first scene where the timer starts but then the code and the display won't carry over to the next scene.

My code is attached to a text element that is inside the canvas, looking into it, this may be part of my issue but I'm not sure how I'm able to get around it.

Here is my code;

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class CountDown : MonoBehaviour {
 
     public GameObject timeTF;
     public GameObject alertReference;
 
     void Start () {
         DontDestroyOnLoad(transform.gameObject);
         timeTF.GetComponent<Text>().text = "120";
         InvokeRepeating("ReduceTime", 1, 1);
     }
     void ReduceTime()
     {
         if (timeTF.GetComponent<Text>().text == "1")
         {
             SceneManager.LoadScene("lose");
         }
 
         timeTF.GetComponent<Text>().text = (int.Parse(timeTF.GetComponent<Text>().text) - 1).ToString();
     }    
 }

And here is the hierarchy of the scene that it's been placed into;

alt text

There is mention of DontDestroyOnLoad not working on child, and only on parents, I don't understand the limitations of it and would appreciate some help and advise, either on how to get the current method I'm using to work, or a different way to carry a time between different scenes. Thanks!

unity.jpg (182.3 kB)
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
3
Best Answer

Answer by mrpmorris · Jul 24, 2016 at 09:05 AM

Why not have a static property on a static class that keeps the current timer value and is reset when you start the game on the first level?

 public static class GlobalCountDown
 {
     static DateTime TimeStarted;
     static TimeSpan TotalTime;
 
     public static void StartCountDown(TimeSpan totalTime)
     {
         TimeStarted = DateTime.UtcNow;
         TotalTime = totalTime;
     }
 
     public static TimeSpan TimeLeft
     {
         get
         {
             var result = TotalTime - (DateTime.UtcNow - TimeStarted);
             if (result.TotalSeconds <= 0)
                 return TimeSpan.Zero;
             return result;
         }
     }
 }

When your first level starts just do this

 GlobalCountDown.StartCountDown(TimeSpan.FromMinutes(5));
Comment
Add comment · Show 7 · 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 ethranes · Jul 24, 2016 at 11:42 AM 0
Share

That sounds very useful, where can I find out more on how to do that?

avatar image ethranes · Jul 24, 2016 at 05:02 PM 0
Share

Thanks I think I'm going to try and use this method that you've posted, I'm just trying to get your example code to work at the moment, getting a compile error 'A get or set accessor expected' at the moment

avatar image mrpmorris ethranes · Jul 24, 2016 at 06:59 PM 0
Share

Just updated the code, try now

avatar image ethranes mrpmorris · Jul 27, 2016 at 07:32 PM 0
Share

Thanks, there are no compile errors now when I build. I can't get the behavior I'm expecting though. I've added some extra code to try and switch the scene when the timer reaches 0

 public static TimeSpan TimeLeft
 {
     get
     {
         var result = DateTime.UtcNow - TimeStarted;
         if (result.TotalSeconds <= 0)
             return TimeSpan.Zero;
         Scene$$anonymous$$anager.LoadScene("Lose");
         return result;
     }
 }

and I've added

 GlobalCountDown.StartCountDown(TimeSpan.FromSeconds(1)); 

to my void update but I'm not getting any reaction.

Is it easy to see what I'm doing wrong in this case?

Show more comments
avatar image ethranes · Jul 28, 2016 at 09:44 PM 0
Share

Using this method I'm not able to set a time in seconds in my first scene that will carry over to future scene.

The next issue that I'd like to solve is to show the amount of time left in a string so that I can display the time left to the player, I'm not sure how to do this, I've tried some variations using TimeSpan.ToString(); or text.text = TimeSpan.FromSeconds; but I can't get anything to compile without errors, any help or advice with displaying the score would be very helpful

avatar image mrpmorris ethranes · Jul 29, 2016 at 07:06 AM 0
Share
 GlobalCountDown.StartCountDown(TimeSpan.FromSeconds(30));

To get the remaining time either

 //For seconds, with a leading zero
 string timeLeft = GlobalCountDown.TimeRemaining.TotalSeconds().ToString("D2");
 // or for $$anonymous$$utes + seconds
 string timeLeft = GlobalCountDown.TimeRemaining.ToString("mm:ss");

 

Then look up the OnGUI method for how to display it on screen

avatar image
1

Answer by Arshia001 · Jul 24, 2016 at 09:00 AM

I'd put separate timers (logic, script, graphics, everything) into every scene. You could have a script with the express purpose of carrying information over to the next scene. Something like this:

 using UnityEngine;
 using System.Collections;
 
 public class InterSceneState : MonoBehaviour
 {
     public float TimeRemaining;
 }

Then, when one scene is to unload and the next is to load, you could do this:

 var GO = new GameObject("State");
 var ISS = GO.AddComponent<InterSceneState>();
 ISS.TimeRemaining = m_timeRemaining; // with m_timeRemaining being whatever way you're keeping the remaining time
 DontDestroyOnLoad(GO);
 Application.LoadLevel(...);

Then, in the timer script's start function which runs when the next scene is loaded:

 var GO = GameObject.Find("State");
 var ISS = GO.GetComponent<InterSceneState>();
 m_timeRemaining = ISS.TimeRemaining;
 Destroy(GO);

And there you have it.

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Slider to modify a countdown time in another scene 0 Answers

Distribute terrain in zones 3 Answers

Question Regarding Don'tDestroyOnLoad Object 1 Answer

Multiple EvenetSystems in Scene - only have 1 after searching though 1 Answer


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