• 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 paaulinsky · May 27, 2015 at 10:21 AM · healthbar

Problem creating health bars for enemies.

Hi everyone!

I've been working with Unity for a few days, trying to make a 3rd person shooter. I've been having a lot of issues, but I've been able to resolve them searching for similar issues people found earlier.

My game is wave based, similar to the zombie mode of COD Black Ops (you kill all the zombies, spawn new wave). What I want to do is display a health bar above every enemy since it is instantiated.

The health bar is a prefab, and it consists of an empty GameObject with a script. The health bar has two child Sprites, representing total health and actual health. The prefab is instantiated when an Enemy is created, but the healthbar is not showing.

NOTE: I made a general class Enemy, as I was planning to create different kind of enemies, and then another class extending from it.

Enemy class:

 public class Enemy : MonoBehaviour {
     public int health;
     public int maxHealth;
     public float speed;
     public int munnyQueSuelta;
 
     public int followDistance;
     public int shootDistance;
 
     //referencia a Player
     public GameObject playah;
 
     //referencia a otros objectos
     public GameObject copper;       //prefab
     public GameObject gold;         //prefab
     public GameObject barraVida;    //prefab
 
     void Start () {
         GameObject healthBar = Instantiate(barraVida, transform.position + new Vector3(0f, 1.75f, 0f), transform.rotation) as GameObject;
         healthBar.transform.parent = this.transform;
     }
     
     // Update is called once per frame
     protected void Update () {
         if (maxHealth < health) {
             health = maxHealth;
         }
 
         if (health == 0) {
             Destroy (this.gameObject);
             sueltaLaPasta();       //instantiates coins, no problem with it
         }
     }
 
     public void gotHit(int damage) {
         this.health -= damage;
     }
 }

This is the hierarchy of the HealthBar Gameobject

alt text

I don't even know if this is the correct way to do it, but the only solutions I found were for Unity 4.6, and I didn't find them useful.

Thanks in advance!

EDIT: as an answer to siaran question, this is the inspector of one of the objects. alt text

captura.png (34.1 kB)
captura2.png (20.8 kB)
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 Flaring-Afro · May 06, 2016 at 11:50 AM 0
Share

Line 29 should probably be

 if (health <= 0) {

If your enemies get damaged more than their current health, your code would do nothing.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by siaran · May 27, 2015 at 10:55 AM

Well, let's start with the obvious,

are there SpriteRenderer components attached to the TotalHealth and Health objects?

Comment
Add comment · Show 7 · 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 paaulinsky · May 27, 2015 at 03:16 PM 0
Share

Yes, each one has it's own Sprite Renderer activated from start.

avatar image siaran · May 28, 2015 at 10:50 AM 0
Share

Allright. Well, next question, are you sure they are facing the camera properly?

Can you see sprites in the scene/game view if you drag your object into the scene (outside of play mode)?

avatar image paaulinsky · May 28, 2015 at 03:32 PM 0
Share

If I drag the prefab to the scene it is visible. It also has a little script so it always has the same rotation as the camera, making it always perfectly visible for the camera, and it works fine.

If it is useful, I checked if the health bar is even instantiated on runtime by looking at the hierarchy, and I noticed that the Enemy object has no child object, when it should have the health bar as a child.

avatar image siaran · May 31, 2015 at 07:29 PM 0
Share

So from your comment, is the healthbar not instantiated or is the parent object not set properly?

avatar image siaran · May 31, 2015 at 07:51 PM 1
Share

So the healthbar isn't instantiated? So your Start() method isn't called?

The only times that has happened to me was when I accidentally overwrote it in a child class...

Also in this case you may want to write

 GameObject healthBar = (GameObject) Instantiate (//not writing this);

ins$$anonymous$$d of

 GameObject healthBar = Instantiate(//again not writing this) as GameObject;

(The difference is that the 1st method throws an exception if a cast is impossible whereas the second just sets the reference to null).

Show more comments
avatar image
0

Answer by EIQUY3 · May 06, 2016 at 11:27 AM

You should parent your health bar to the canvas or it wont appear in the game. HealthBar.transform.SetParent(Canvas, false);

Note: Canvas is canvas in your game and you need reference of it in your code.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Splitscreen w/ different GUIs on each camera 2 Answers

Healthbar not always working, someone please help! 2 Answers

GUI.HorizontalScrollbar won't change it's height 1 Answer

GuiTexture above 3D objects 1 Answer

NullReferenceException: Object reference not set to an instance of an object 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