Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Countdown timer resets when clicked "Launch" button

Discussion in 'Scripting' started by Stiefo.o, Aug 21, 2014.

  1. Stiefo.o

    Stiefo.o

    Joined:
    Jul 11, 2013
    Posts:
    14
    Hi guys,

    I'm getting this problem, I wrote the code below and what he should do is to apply a force until it finishes a countdown that if I put 5 sec (just to not stay long) and also put a button on the GUI called "Launch". So far I think I made the correct logic, however when I put this script in a cube, and click on "Launch" the timer changes from 5 to 0 without counting backwards, what I want is to continue applying pressure until the time runs out .

    Can anyone tell me what I'm doing wrong?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Launcher : MonoBehaviour {
    5.  
    6.     public float timer;
    7.  
    8.     void OnGUI()
    9.     {
    10.         if(GUI.Button (new Rect (10, 10, 100, 90), "LAUNCH"))
    11.         {
    12.             Launch();
    13.         }
    14.     }
    15.  
    16.     void Launch()
    17.     {
    18.         while(timer > 0)
    19.         {
    20.                 rigidbody.AddForce (Vector3.up * 5);
    21.                 timer = timer - Time.deltaTime;
    22.         }
    23.         Debug.Log ("Game Over");
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.  
    29.     }
    30. }
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  3. Stiefo.o

    Stiefo.o

    Joined:
    Jul 11, 2013
    Posts:
    14
    Cool, but how'll implement this?
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  5. Stiefo.o

    Stiefo.o

    Joined:
    Jul 11, 2013
    Posts:
    14
  6. Imagineer

    Imagineer

    Joined:
    Mar 16, 2014
    Posts:
    1
    The while loop is executed in 1 frame. So it runs the code until timer is 0 and then go to the next frame. you can use a if statement for this: if(timer > 0)

    If this helped you please mark as solution or +1 ;)
     
    Stiefo.o likes this.
  7. Stiefo.o

    Stiefo.o

    Joined:
    Jul 11, 2013
    Posts:
    14
    I tried it with "if (timer> 0)" but time and force can only be applied if I hold clicked button release. All I want is when I click the button once the timer starts counting down and the force is applied until the time runs out.
     
  8. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    you need to do a coroutine like this:

    Code (CSharp):
    1. IEnumerator CountDown (float delay) {
    2.         while (timer > 0) {
    3.             //ADD FORCE
    4.             timer -= Time.deltaTime;
    5.            
    6.             yield return new WaitForSeconds (delay);
    7.         }
    8.     }
    9.  
    then when your button is pressed call it by:

    Code (CSharp):
    1. StartCoroutine (CountDown (1.0f));
     
    Stiefo.o likes this.
  9. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    I havent't tested it so there could be a error, but hopefully you get the gist of things
     
    Stiefo.o likes this.
  10. Stiefo.o

    Stiefo.o

    Joined:
    Jul 11, 2013
    Posts:
    14
    That's exactly what I wanted, thank you!