Search Unity

Timer issue

Discussion in 'Scripting' started by Simonxzx, Mar 25, 2015.

  1. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Hi everyone, i'm developing a game both for Android and iOS. When the player starts the game a menu appears, with the relative button 'start' to make it begin, a following scene of loading (i'm not a pro user so i made it by myself) and then, finally, the game. The only problem is that the timers in the last scene don't start when this scene is loaded, but suddenly, when the player opens the game at the menu scene. This fact creates me a lot of problems, because those timers must start their count only when the last scene of the game has loaded. How can I solve this problem? Thank you very much.
     
  2. milkshakeiii

    milkshakeiii

    Joined:
    Feb 16, 2013
    Posts:
    9
    If you would like to access the time elapsed since the scene loaded, have you tried Time.timeSinceLevelLoad ?
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Difficult to help without knowing how you timers are built.
     
  4. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Hi guys, and thank you for your replies. I tried to use Time.timeSinceLevelLoad, but when i debugged it with Debug.log() I realized it isn't precise. I want my timers to recognize if the seconds elapsed since the level loaded are 0.50 and, then, if they are three multiples so that I can instantiate the objects i want to (in this case textures) at these precise seconds.

    BoredMormon, this is the code, thank you.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RandomScrolling : MonoBehaviour {
    5.     public GameObject[] backGrounds = new GameObject[3];
    6.     public float seconds = 0.0f;
    7.     private float force = 235;
    8.     private float vectorpos = -7f;
    9.     public float vectorpos_2 = -6.8f;
    10.     private float delay = 0.50f;
    11.  
    12.     // Update is called once per frame
    13.     void FixedUpdate (){
    14.         delay = 0.50f * -Time.fixedTime;
    15.         seconds = Time.fixedTime;
    16.  
    17.         if (delay == 0)
    18.         {
    19.             GameObject backClone = Instantiate(backGrounds[2], new Vector3(vectorpos, 4, 1), Quaternion.identity) as GameObject;
    20.             backClone.transform.SetParent(gameObject.transform);
    21.             backClone.GetComponent<Rigidbody2D>().AddForce(transform.right * force);
    22.         }
    23.         if (seconds % 3 == 0 && seconds != 0)
    24.         {
    25.             int randomInstantiate = Random.Range(0, 2);
    26.             GameObject backClone = Instantiate(backGrounds[randomInstantiate], new Vector3(vectorpos_2, 4, 1), Quaternion.identity) as GameObject;
    27.             backClone.transform.SetParent(gameObject.transform);
    28.             backClone.GetComponent<Rigidbody2D>().AddForce(transform.right * force);
    29.         }
    30.     }
    31. }
    32.  
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Direct comparison of floats will never work. Use > instead, and a bool to ensure it only happens once.

    Or use a coroutine or invoke repeating.
     
  6. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    How to make a control with the bool you said?
     
  7. milkshakeiii

    milkshakeiii

    Joined:
    Feb 16, 2013
    Posts:
    9
    Hello Simon, here is a script using time floats (seconds) and boolean logic. It calls DoInstatiation at the 0.5 second mark, and every multiple of 3, which I believe is what you intended. Hope this can help you write your application, and maybe you can learn something from it. :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SimonInstantiator : MonoBehaviour
    5. {
    6.     public float firstInstantiate = 0.5f;
    7.     public float everySuccessiveInstantiate = 3.0f;
    8.  
    9.     private float lastInstantiate = 0;
    10.  
    11.     void Update ()
    12.     {
    13.         float currentTime = Time.timeSinceLevelLoad;
    14.         //if more than firstInstantiate seconds have elapsed,
    15.         //and we have instantiated before firstInstantiate,
    16.         //then we are due for the first instantiate
    17.         bool dueForFirstInstantiate = (currentTime > firstInstantiate &&
    18.                                       lastInstantiate < firstInstantiate);
    19.         //if it has been three seconds since our last instantiate,
    20.         //or with the same logic as above, we are due for the 3 second mark
    21.         //instantiate, then we are due for a successive instantiate
    22.         bool dueForSuccessiveInstantiate = (currentTime - lastInstantiate >
    23.                                            everySuccessiveInstantiate ||
    24.                                            (currentTime > everySuccessiveInstantiate &&
    25.                                             lastInstantiate < everySuccessiveInstantiate));
    26.  
    27.         if (dueForFirstInstantiate || dueForSuccessiveInstantiate)
    28.         {
    29.             DoInstantiation();
    30.             lastInstantiate = currentTime;
    31.         }
    32.     }
    33.  
    34.     private void DoInstantiation()
    35.     {
    36.         Debug.Log ("The time is " + Time.timeSinceLevelLoad + " - Do instantiation here.");
    37.     }
    38. }