Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Matt 5 · Jan 06, 2012 at 10:01 PM · guihealthbar

I want to add a health bar to existing code

I already have code that handles the enemy's health what I want to do is incorporate a health bar system that would be managed by the current code. If anyone could please help me with this I would really appreciate it I've checked all the other related topics to this issue but all I see is separate code that would be on there own, what I need is something that is used within my current code.

Please someone help.

Here is my code:

using UnityEngine; using System.Collections;

public class DamagableObject : MonoBehaviour {

 public bool m_Invincible = false;
 public float m_MaxHealth = 100f;
 
 [SerializeField]
 public float m_Health = 100f;
 
 public DecayingObject m_DeathEffectPrefab;
 public DecayingObject m_RemainsPrefab;
 public string m_DeathAnimation;
 
 /*public virtual float health
 {
     set
     {
         if (!m_Invincible ||  m_Health < value)
         {
             m_Health = value;
         }
         if (m_Health <= 0)
         {
             Death();
         }
         else if (m_Health > m_MaxHealth)
         {
             m_Health = m_MaxHealth;
         }
     }
     
     get
     {
         return m_Health;
     }
 }*/
 
 public virtual void DealDamage(float val, Vector3 pos, Vector3 normal)
 {
     if (val > 0f && !m_Invincible)
     {
         m_Health -= val;
         if (m_Health <= 0)
         {
             Death();
         }
     }
 }
 
 public virtual void Heal(float val)
 {
     if (val > 0f)
     {
         m_Health += val;
         if (m_Health > m_MaxHealth)
         {
             m_Health = m_MaxHealth;
         }
     }
 }
 
 public float GetHealth()
 {
     return m_Health;
 }
 
 protected void Start() {
     bool error = false;
     if (m_MaxHealth <= 0)
     {
         Debug.LogError("Max health can't be less than or equal to 0");
         error = true;
     }
     if (m_Health > m_MaxHealth || m_Health <= 0)
     {
         Debug.LogError("Invalid health range expected 0-" + m_MaxHealth + " got " + m_Health);
         error = true;
     }
     if (error)
     {
         Debug.Break();
     }
 }    
 
 
 
 protected void Death()
 {
     if (m_DeathEffectPrefab != null)
     {
         Instantiate(m_DeathEffectPrefab, transform.position, transform.rotation);
     }
     if (animation != null && animation[m_DeathAnimation] != null)
     {
         animation.CrossFade(m_DeathAnimation, 0.5f, PlayMode.StopAll);
         if (m_RemainsPrefab != null)
         {
             StartCoroutine("WaitForDeathAnimation", animation[m_DeathAnimation].length);
         }
     }
     else
     {
         if (m_RemainsPrefab != null)
         {
             Instantiate(m_RemainsPrefab, transform.position, transform.rotation);
         }
         Destroy(gameObject);
     }
 }
 
 protected IEnumerator WaitForDeathAnimation(float time)
 {
     yield return new WaitForSeconds(time);
     Instantiate(m_RemainsPrefab, transform.position, transform.rotation);
     Destroy(gameObject);
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Matt 5 · Jan 06, 2012 at 10:04 PM 0
Share

I got this code from a friend and altered it I just need help with this health bar issue

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Anxo · Jan 06, 2012 at 10:11 PM

Create a var that holds a prefab of a health bar for the enemy prefab within your code, then just have its width be controlled by the var that holds that enemies health.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Matt 5 · Jan 06, 2012 at 10:13 PM 0
Share

I'm a little new to this would you be able to give me an example?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GuiTexture above 3D objects 1 Answer

GUI healthbar 3 Answers

Detect if any GUI is being displayed on-screen? 1 Answer

percentage health bar 1 Answer

Shrink GUI 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges