Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Counting loop problem....Java

Discussion in 'Scripting' started by Kayden007, Sep 12, 2012.

  1. Kayden007

    Kayden007

    Joined:
    Jan 14, 2011
    Posts:
    92
    I'm planning on making my own timing system with seconds, minutes, hours.....etc (in java language), but I'm trying to make the "Seconds" count to 60 then start back at 1 and then if seconds =60 then minute=+1


    But when I type in this
    Seconds = Seconds + 1;

    It counts...but in tens .

    Code :

    var Seconds = 0;
    private var SecondCalculator = 0;


    function Start () {

    }

    function Update () {

    Seconds = Seconds + 1;

    }

    Can someone help me with this please (Checked the "Newbie guide to Unity Javascript (long)" and read the whole thing twice)
     
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Update() gets called every frame, not every second, so if you're getting 40 FPS, then it will count up by 40 every second. Instead you should use Time.deltaTime, which is the time in seconds since the last frame. Like this:

    function Update() {
    Seconds = Seconds + Time.deltaTime
    }
     
  3. Kayden007

    Kayden007

    Joined:
    Jan 14, 2011
    Posts:
    92
    Thanks, but I stays at 0
     
  4. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    That's because it's an integer.

    Code (csharp):
    1. var seconds : float = 0;
    2. function Update() {
    3.   seconds += Time.deltaTime;
    4. }
     
  5. Kayden007

    Kayden007

    Joined:
    Jan 14, 2011
    Posts:
    92
    Thanks a lot, at first it didn't work so I made a new java file and it worked!
    I then copied everything over to the old one but still didn't work. So I deleted the old one, which was named "Time" and replaced it with the new one "NewBehaviourScript", I later renamed "NewBehaviourScript" to "Time" and it stopped working. Guess it was the file name the whole time. Anyway thanks a lot makeshiftwings and Loius :D
     
  6. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    File name == class name.

    By naming the file "Time", you're overwriting the Time class in Unity.

    This makes it difficult to use Unity's Time class instead of your own. :p
     
  7. Kayden007

    Kayden007

    Joined:
    Jan 14, 2011
    Posts:
    92
    ohhh, now I see thanks!
     
  8. Kayden007

    Kayden007

    Joined:
    Jan 14, 2011
    Posts:
    92
    Finally finished the advanced time system I've been working on. Complete dates with even the number of days for each individual month and even for leap years.
    Thanks a lot. Let me know if you any of you may want it