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
  4. Dismiss Notice

Question Update is 5x too fast

Discussion in 'Editor & General Support' started by Cosmology27, Jan 5, 2023.

  1. Cosmology27

    Cosmology27

    Joined:
    Jul 11, 2019
    Posts:
    61
    I've tried update and fixedupdate, both are running about five times too fast. I haven't messed with the timescale or anything at all. All I'm doing is a simple counter within the update (or fixedupdate) where the counter goes down, and if it reaches zero then it will spawn an enemy, then go back up to its spawn time. FixedUpdate runs 50 times a second, and yet it's going at about 250 a second. I set the TimeBetweenSpawns to 250, and it spawned an enemy about every second. When I set it to 2,500 then it spawned one every 10 seconds, so clearly it's running five times faster than it should. My scripts are very simple so far, I'm just starting, and I've never had my updates run this fast before, so I'm very confused. Here's the script.
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         HowMuchTime -= 1;
    4.         if (HowMuchTime <= 0 && HowManyToSpawn >= 1)
    5.         {
    6.             HowManyToSpawn -= 1;
    7.             HowMuchTime = TimeBetweenSpawns;
    8.             var NewEnemy = Instantiate(EnemyType, transform.position, Quaternion.identity);
    9.         }
    10.     }
    Also I'm sorry for the tags. This site honestly has the worst tagging system I've ever seen. Every tag I've entered doesn't exist, and none of the recommended make any sense for this question.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,917
    It's because you're counting frames in your code, so it's rate is entirely dependant on your frame rate.

    Use a timer by incrementing a float value by
    Time.deltaTime
    , which is the actual time between frames. This is a fundamental Unity principle.
     
    Cosmology27 likes this.
  3. Cosmology27

    Cosmology27

    Joined:
    Jul 11, 2019
    Posts:
    61
    From what I understand, what you said is correct only in the case of it being in Update. When I put it in FixedUpdate it should only have 50 frames a second, right? So why is it counting 250 frames a second in FixedUpdate too? Thanks for the help!
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,917
    I'm gonna say 'not sure why' off the top of my head, but in any situation where you want to measure time reliably you should be using the Time class and it's various deltaTime properties, rather than counting frames of Unity's magic methods.
     
    PraetorBlue and Cosmology27 like this.
  5. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Is that counter static, and do you have five instances of the spawner? That would make it count down five times too quickly.

    If not, then showing the entire script would be helpful. You said you're using FixedUpdate, but that code block is in Update :p

    You should check that you haven't done something to the fixed timestep. Check the settings in here:

    upload_2023-1-5_9-39-8.png

    As spiney said, though, there are very few reasons to count frames instead of seconds.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Nothing in your code is doing anything to pin this timer to a "number of seconds" so any comparison to real time passage of seconds is useless. Your current timer is counting frames rather than an amount of time. Frames are not a consistent measure of time. Framerates fluctuate wildly constantly. They will also be very different on different computers and hardware and under different software load.

    Long story short - don't use frame count as a timer. You can convert your timer from counting frames to counting seconds using
    Time.deltaTime
    .

    Simple example:
    Code (CSharp):
    1. float timer = 0;
    2.  
    3. void Update() {
    4.   timer += Time.deltaTime;
    5.  
    6.   Debug.Log($"Current time is: {timer}");
    7. }
     
  7. Cosmology27

    Cosmology27

    Joined:
    Jul 11, 2019
    Posts:
    61
    Thanks all, I switched to taking off time.deltatime and it works properly now.
    I'm still confused why the FixedUpdate was going at 250 a second. I input it in the inspector to get the numbers like 250 and 2500, but it was consistently going about 5x faster than it should, even in FixedUpdate. Either way I'll try to use time.deltatime more often now. I've never really understood what the value of it was, but what you've all said makes sense. Thanks!