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

Updating the position of enemy hp bar every turn?

Discussion in 'Scripting' started by anydayzz, Feb 17, 2016.

  1. anydayzz

    anydayzz

    Joined:
    Feb 13, 2016
    Posts:
    111
    Would it be better to check if the hp bar needs to be updated before updating it or should I just update the hp bar positions every frame without checking? Which option is best for performance? I'm making a very fast paced game and the health bar changes position about 3/4 of the updates and 1/4 it doesn't need to be updated. Is this meaningless optimization?
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I dont think this matters. The screen is going to redraw it's self every frame anyways. Changing a value from 15% to 14% is not going to affect performance at all. The engine is still going to draw it.
     
    anydayzz likes this.
  3. anydayzz

    anydayzz

    Joined:
    Feb 13, 2016
    Posts:
    111
    Thank you a lot.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    If you're talking about the UI, the cost will not quite be zero, maybe: If nothing at all changes in the Canvas, it's smart enough to not rebuild its mesh. However, if any UI element moves, they're all rebuilt. But even so, this is a tiny, tiny cost, and I'd be shocked if it ever caused a measurable difference.

    Ultimately, this is definitely a case a "premature optimization". Don't worry about such small details until you have run the profiler and determined that A) they are a problem, and B) to what extent they are a problem. If you optimize before profiling, you'll never know whether your optimization was needed, and you'll also never know how much your optimization saved (if anything).
     
    anydayzz likes this.