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 Ittoryu · Dec 22, 2015 at 08:37 PM · timerwaitforsecondsreloadtimer countdowntimer-script

Reload level after timer hits 0

Hi,

First of all, thanks for any answer that I might (or might not!) get in this thread. I've recently started using Unity and completed the "roll a ball" tutorial. I've added a basic timer script that will count from 30 to 0 and reload the level when 0 will hit.

I'd like to add a basic "You lost!" text and reload the level after 5 seconds (or so), but I can't seem to be able to correctly add the Coroutine and the subsequent WaitForSeconds inside an "if timer hits 0 then...".

Below is my very basic code for the timer:

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 using System.Collections;
 
 
 public class myTimer : MonoBehaviour {
 
     public float myTimer = 30;
     public Text timerText;
 
 
 
     // Use this for initialization
     void Start () {
     
         timerText = GetComponent<Text> ();
 
     }
 
 
     
     // Update is called once per frame
     void Update () {
         myTimer -= Time.deltaTime;
         timerText.text = myTimer.ToString ("f0");
         print (myTimer);
 
         if (myTimer <= 0) {
 
             SceneManager.LoadScene (0);
 
         }
 
 
         }
 
     
 }


Thank you all for your help!

Comment
Add comment · Show 5
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 gjf · Dec 22, 2015 at 04:50 PM 0
Share

where's your coroutine start code?

avatar image Ittoryu gjf · Dec 22, 2015 at 05:17 PM 0
Share

Sorry, I should have been more specific. At the moment, the code does not have a couroutine called, because I did put it here:

 void Start () {
     
         timerText = GetComponent<Text> ();
         StartCoroutine ("waitFiveSeconds");
     }

but the problem is that I don't know where to put the WaitForSeconds bit as, if I leave it inside the "if" part, it won't work:

 void Update () {
         myCoolTimer -= Time.deltaTime;
         timerText.text = myCoolTimer.ToString ("f0");
         print (myCoolTimer);
 
         if (myCoolTimer <= 0) {
 
             print (youLost);
             IEnumerator waitFiveSeconds () {
                 yield return new WaitForSeconds (5);
                 Scene$$anonymous$$anager.LoadScene (0);
             }

Thanks!

avatar image Ittoryu gjf · Dec 22, 2015 at 05:20 PM 0
Share

Also, sorry if some part of the code are a bit of a mismatch, but I am trying a billion combination to make this thing work :)

avatar image gjf Ittoryu · Dec 22, 2015 at 06:59 PM 0
Share

you're not a million miles away...

first of all, take the coroutine out of Update() and don't start it from Start()!

the code will work if you start it when the player has lost so, in the original, replace your load scene call with the StartCoroutine() call and all should be well.

you might consider making the wait time a parameter so that the code can be reused, although for now (to get it working) this will obviously be fine ;)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zeldax54 · Dec 23, 2015 at 04:11 AM

May be this:

  public float myTimer = 30;
  public Text timerText;
  public float showsecond=5f;
  bool _isshow;
 
 
 
  // Use this for initialization
  void Start () {
  
      timerText = GetComponent<Text> ();
 
  }


  void Update () {
      myTimer -= Time.deltaTime;
      timerText.text = myTimer.ToString ("f0");
      print (myTimer);
    if(mytTimer<=showsecond && _isshow)
   {
   timerText.text="LOST LOST LOST";
   _isshow=false;
  }
      if (myTimer <= 0)  
          SceneManager.LoadScene (0);
      
 
 
      }

Comment
Add comment · Show 2 · 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 Ittoryu · Dec 23, 2015 at 02:21 PM 0
Share

Thank you for the above script. I've tried it and it seems to work fine; however, it does not display the "You lost" text and it just reload the level as soon as the timer hits 0. :(

Thanks again for your help!

avatar image zeldax54 Ittoryu · Dec 23, 2015 at 03:43 PM 0
Share

oh am sorry change this. $$anonymous$$y eeror is managing the bool variable.

    if(mytTimer<=showsecond && !_isshow)
    {
    timerText.text="LOST LOST LOST";
    _isshow=true;
   }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make reverse countdown timer in unity? 1 Answer

Speed up timer 1 Answer

How do you make a countdown timer that will lose time if you press a button? 1 Answer

Stop and Pause Timer 2 Answers

timer not ticking down 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