c# - Unity3d health bar GUI element -


i want create health bar using information i'm displaying in guitext. behaviour want health decrease on time (like countdown timer), increase small amount whenever player collects object.

here current code:

using unityengine; using system.collections;  public class timetext : monobehaviour {      public guitext timetext;     //countdown     public float timer = 99.00f;      // update called once per frame     void update ()      {         timer -= time.deltatime*5;          timetext.text = "health: " + timer.tostring("0");      } } 

here health bar like: enter image description here

as @joeblow mentioned above, avoid old gui mechanics. super heavy, crude, , not performance-helpful.

@serlite broke down question you. need.

for health-bar, suggest using unity ui's image component. use image want (possibly same 1 posted in question?) health bar , change fillamount based on timer variable (though personally, rename "health" represent purpose better).

it looks you've taken care of reducing health on time. add health, use colliders triggers in scene, , ontriggerenter or of similar default monobehavior methods on appropriate objects.

i suggest going through couple tutorials online, better understanding of programming logic in unity. hope helps!


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -