Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DivaliciousGeek · Sep 11, 2018 at 01:57 PM · unity 5gameobjectenemy health

How to make multiple Enemy Health Bars to work?

Hello everyone! I'm in dire need of some help. I currently working on a beat 'em up and I manage to get the enemy health bar to work, however, whenever I place more than one enemy within the scene only one is displayed over being shown while the others are blank. Here's the tutorial I've been following => ( https://www.youtube.com/playlist?list=PLdAZlT2Z_lOv3jQXAjXNqZXsp3Tf5Cjhk ) and I'll also post my code for everyone to see...

 public class Stats: MonoBehaviour
 {
     // ~~~~~~~~~~~~~~~~Variables~~~~~~~~~~~~~~~~
     #region
     // ~~~~~~~~~~~~~~~~Public~~~~~~~~~~~~~~~~
     public float health;
     public float strtHsd; //Starting Health
     public bool dispUI; //Display UI
     public Slider hsdSlider; // Health slider
     public GameObject hsdUI; // HealthUI
     public bool enemy2Win;
     // ~~~~~~~~~~~~~~~~Private~~~~~~~~~~~~~~~~
     //private object GameManager; // Just add a private variable.
 
     // ~~~~~~~~~~~~~~~~Others~~~~~~~~~~~~~~~~
         #endregion
 
     void Awake()
     {
         health = strtHsd;
     }
 
     void Update()
     {
         if (gameObject.tag == ("Player"))
         {
             hsdUI = GameObject.FindGameObjectWithTag("PlayerHealthUI");
             hsdSlider = hsdUI.gameObject.transform.GetChild(0).GetComponent<Slider>();
             if(hsdSlider.maxValue == 0)
                 hsdSlider.maxValue = strtHsd;
                 hsdSlider.value = health;
         }
         if (gameObject.tag == ("Enemy") /*&& dispUI == true*/)
         {
             hsdUI = GameObject.FindGameObjectWithTag("EnemyHealthUI");
             hsdSlider = hsdUI.gameObject.transform.GetChild(0).GetComponent<Slider>();
             if (hsdSlider.maxValue == 0)
                 hsdSlider.maxValue = strtHsd;
             hsdSlider.value = health;
         }
         else if (gameObject.tag == ("Enemy"))
         {
             hsdUI = null;
             hsdSlider = null;
         }
         if (health <= 0)
         {
             //Destroy(gameObject.transform.parent.gameObject); If there is a gameobject that does not have a parent attached, the code has nothing to destroy.
             Destroy(gameObject.transform.gameObject);//Instead of destroying just the sprite, we'll be destroying the enemy spawner location.
         }
 
         if (enemy2Win == true)
         {
             GameManager.instance.GameOver();
         }
     }
 
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by tormentoarmagedoom · Sep 11, 2018 at 02:16 PM

Good day.

I did not read your code carefully to understad what are you doing. But for your question i can undesrstand you have some basi structure problems..

First, all enemies must have its own script, and irt ts own healthbar, so each script "talks" with its healthbar.

Soooo, i see this code:

  if (gameObject.tag == ("Enemy") /*&& dispUI == true*/)
          {
              hsdUI = GameObject.FindGameObjectWithTag("EnemyHealthUI");

Here, you are saying this: If "I'm an enemy", the hsdUI is the first object found in the scene with tag "EnemyHealthUI"

As you cna imagine this is 100% incorrect, there are so many healthbars in the scene, so it will not work, i need that each enemy script talks only with its own bar, you should use something like:

              hsdUI = gameObject.transform.Find("HealthBar");ç

To find the healthbar child of the object..

I strongly recommend to learn how to acces all components, before commence doing a complex tutorial, or you will get lost like now. Go watch some basic tutorials about referencing, accesing, and seting variables.

Bye!

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
avatar image
0

Answer by I5 · Sep 11, 2018 at 02:34 PM

Without a screenshot, I"m not sure what you're seeing, but based on the code I would guess that your hsdsSlider(s) may overlapping. To see if that's the case you could try setting the hsdSlider transform position in Awake to something unique, maybe based on an "id" inspector var. Ex:

 public int id = 1;  //set this to a unique value for each character in the inspector/editor

then in Awake set the hsdSlider position based on the id. Ex:

 void Awake() {
    hsdSlider = hsdUI.gameObject.transform.GetChild(0).GetComponent();
    hsdSlider.transform.position = hsdSlider.transform.position * id;//not final solution, just to prevent overlapping
 }

also, just a suggestion for improving performance, don't use "FindGameObjectWithTag" in Update. Get a reference to the object once in Awake (or maybe better to do in Start), and then use that reference in Update.

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.

Follow this Question

Answers Answers and Comments

215 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to select gameobjects on android by touching on them? 1 Answer

Help needed on c# code which all of you will find basic apart from me 1 Answer

cannot drag script to player.Guitext error,cannot drag player script to the player in hierarchy 2 Answers

Model not rendered on Android Lollipop 5.0.1 0 Answers

Instantiation and Deletion very slow... HELP.. 3 Answers


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