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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with a script to make a countdown

Discussion in 'Scripting' started by Real3dvision, Sep 17, 2015.

  1. Real3dvision

    Real3dvision

    Joined:
    Jul 3, 2015
    Posts:
    13
    I try to make a script to make a countdown but it fail. i think that the problem is that contador variable is float and i need a integer...But if i define contador like integer then i have a problem with time.deltatime.Could you somebody help me?
    Regards.



    using UnityEngine;
    using System.Collections;

    public class cuenta_atras : MonoBehaviour {


    public float contador=3f;



    // Use this for initialization
    void Start () {

    }
    // Update is called once per frame}
    void Update ()
    {


    while (contador > 0)
    contador -= Time.deltaTime;
    Debug.Log ("contador"+ contador);

    }

    }
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    First of all, you should always post the errors, because it makes things easier for the other people. Put your code in code tags, you find them under Insert/Code on the comment toolbar. I think your error is like: "Cannot convert float to string", if it is, then the solution is to put a .ToString() after contador so it would look like this : Debug.Log("contador" + contador.ToString());
     
    Real3dvision likes this.
  3. Real3dvision

    Real3dvision

    Joined:
    Jul 3, 2015
    Posts:
    13
    Sorry, is my first thread... the real problema is that if i use this script then unity stay block...and i don´t know which the problem is. I think that it can be for the issue of the variables but i don´t know...
    Thanks a lot.
     
  4. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102
    Hola @Real3dvision (your var names show me that you are spanish xD)

    The problem is that you need to understand that Update method is executed one time every frame. You don't need this line:

    Code (CSharp):
    1. while(contador>0)
    Whit your while, you decrease contador in one frame! But you want to decrease step by step, one step in one frame.

    http://docs.unity3d.com/ScriptReference/MonoBehaviour.Update.html
     
    Real3dvision likes this.
  5. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    You probably want to use if(contador > 0) instead of while.

    Try something like this:

    Code (csharp):
    1.  
    2. if(contador > 0){
    3.     contador -= Time.DeltaTime;
    4.     contador = Mathf.RoundToInt(contador);
    5. }
    6.  
     
    Real3dvision likes this.
  6. Real3dvision

    Real3dvision

    Joined:
    Jul 3, 2015
    Posts:
    13
    Thanks. Do you recomend me to use coroutines then or other method?
     
  7. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102
    No, you only need to decrement "contador" every frame, and Update method is executed every frame. Coroutines are something more complex and your problem is more simple.
     
    Real3dvision likes this.
  8. Real3dvision

    Real3dvision

    Joined:
    Jul 3, 2015
    Posts:
    13
    Thanks
    I test with void lateupdate but it doesn´t work. Could you tell me which is the method?
    I have this code, and with it i get a countdown but i like to stop it with a value of "contador".
    I was looking in the help of unity but i didn´t find a method to make something between frames.
    Regards

    using UnityEngine;
    using System.Collections;

    public class aver : MonoBehaviour {
    public float contador=3f;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame}
    void Update ()
    {
    contador -= Time.deltaTime;
    }

    }
    Regards.
     
    Last edited: Sep 18, 2015
  9. Real3dvision

    Real3dvision

    Joined:
    Jul 3, 2015
    Posts:
    13
    Thanks for you reply, the problem is that i hace to put your code inside a method and i don´t found anyone that it happens between frames.
    Regards.
     
    Last edited: Sep 18, 2015
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Technically Update happens 'between frames'. Update gets called once per frame, after the physics update, but before the frame is rendered.
     
    Real3dvision likes this.
  11. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I used this in one of my games & it worked fine inside the update function:
    Code (CSharp):
    1. // calculate game time, end game at 3 minutes
    2.                 gameTime -= Time.deltaTime;
    3.                 if (gameTime <= 0)
    4.                         Application.LoadLevel (2);
    To display it as whole numbers onscreen instead of the float with a whole string of decimals needs you to format it. The attached is what I did but it won't work for you if you copy/paste it as it is referencing an instance of my manager script but you can see what I was doing with the formatting.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class GameTime : MonoBehaviour {
    5.     Text mytext;
    6.     int minutes;
    7.     int seconds;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         mytext = GetComponent<Text> ();
    12.         int minutes = Mathf.FloorToInt(Manager.instance.gameTime / 60F);
    13.         int seconds = Mathf.FloorToInt(Manager.instance.gameTime - minutes * 60);
    14.  
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if (Manager.instance.gameTime > 0) {
    20.                         int minutes = Mathf.FloorToInt (Manager.instance.gameTime / 60F);
    21.                         int seconds = Mathf.FloorToInt (Manager.instance.gameTime - minutes * 60);
    22.  
    23.                         string niceTime = string.Format ("{0:0}:{1:00}", minutes, seconds);
    24.                         mytext.text = ("Time Left:  " + niceTime);
    25.                 }
    26.     }
    27. }
     
    Real3dvision likes this.
  12. Real3dvision

    Real3dvision

    Joined:
    Jul 3, 2015
    Posts:
    13
    Thanks for you answers!!!
    I write the code with your suggestions and i get it, but i like to stop the execution when the variable contador=0. Could you tell me how can do it?
    Regards.

    upload_2015-9-18_23-4-55.png
     
  13. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You could move the countdown bit into the else statement just above the debug line. That way it only counts down when the timer is above 0
     
    Real3dvision likes this.
  14. Real3dvision

    Real3dvision

    Joined:
    Jul 3, 2015
    Posts:
    13
    Ok, i test it...we got it. Thanks!!!!
     
  15. Real3dvision

    Real3dvision

    Joined:
    Jul 3, 2015
    Posts:
    13
    I am looking for show in the screen the countdown but i have a problem with the guitext...
    Do you know if is an issue with the new version of Unity?
    upload_2015-9-19_8-29-11.png
     
  16. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Real3dvision likes this.
  17. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Don't complicate simple things guys:
    Code (CSharp):
    1.     void OnEnable()
    2.     {
    3.         StartCoroutine(CountDown());
    4.     }
    5.  
    6.     IEnumerator CountDown()
    7.     {
    8.         float TimeTick = 1.0f;          
    9.         int CountDowmFrom = 3;      
    10.      
    11.         for (int i = 0; i < CountDownFrom; i++)
    12.         {
    13.             Debug.Log("Count down from " + (CountDownFrom-i).ToString());
    14.             yield return new WaitForSeconds(TimeTick);
    15.         }
    16.         Debug.Log("Go");  
    17.     }
    18.  
     
    Real3dvision likes this.
  18. Real3dvision

    Real3dvision

    Joined:
    Jul 3, 2015
    Posts:
    13
    First of all thanks for your help.
    I write "using unityengine" but without "..ui". i test it.
    I am learning c# with unity and i am doing a course at the same time that i watch the tutorials of unity...but i like to practise and for that i have this kind of problems..
    Regards
     
  19. Real3dvision

    Real3dvision

    Joined:
    Jul 3, 2015
    Posts:
    13
    Thanks Ironman but i am a novice and i prefer don´t use coroutines still.
    Regards.