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 /
  • Help Room /
avatar image
2
Question by Andrekilik · Oct 04, 2016 at 06:25 PM · c#canvasslider

Problem With Multiple Sliders - Health Bars

Hi everyone, I'm a old Cobol programmer, trying to learn unity for a life change, and got stuck with this issue, already looked up on many posts around here and on stackoverflow, but nothing so far gave me at least an idea of what I'm doing wrong.

I'm working on a little 2D Sidescrolling SHMUP, and I am having a little trouble with Health Bars(I've been following a few online tutorials, both from Unity and from Youtube).

What I'm trying to accomplish: I have a scene with a boss battle, and during this scene, I want to display two Health bars onscreen, one for the boss and one for the player.

What I'm getting: When I create the first Slider Bar, and create the code for it, it works normally. However, when I create the second Slider bar, I get either of the following results: None of the bars Work, or both the boss and the player cause damage on the first bar.

Here's what I've done: In the scene, I've created a Canvas, as a child of the canvas, I've created an Empty gameObject that I've called HUD. As childs of this HUD object, I've created the two Slider UI elements. Like this: alt text

After that I've created an Script to control the sliders, and attached it to the HUD Object. the code is as follows:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class HudControl : MonoBehaviour {
 
     public PlayerBF playerBf;
     public BossShotControl boss;
     public Slider playerSlider;
     public Slider bossSlider;
     private int playerPrevHp, bossPrevHp;
     // Use this for initialization
     void Start () {
         playerBf = GetComponent<PlayerBF>();
         boss = GetComponent<BossShotControl>();
         playerSlider = GetComponent<Slider>();
         bossSlider = GetComponent<Slider>();
         playerPrevHp = playerBf.hp;
         bossPrevHp = boss.bossHp;
     }
     
     // Update is called once per frame
     void Update () {
         if (playerBf.hp != playerPrevHp)
         {
             playerSlider.value -= 1;
             playerPrevHp = playerBf.hp;
         }
         if (boss.bossHp != bossPrevHp)
         {
             bossSlider.value -= 1;
             bossPrevHp = boss.bossHp;
         }
     }
 }

I've also tried changing, the "GetComponent<>()" for (PlayerBF)FindObjectOfType(typeof(PlayerBF)); but to the same results. I've also tried making reference to the sliders inside of the scripts of the bullets(bossbullets and playerbullets), and make them decrease at the same time I cause damage, but again, the results were the same, only one slider taking damage, or both not working.

Thanks in Advance, any help will be appreciated.

hiearchy.png (5.6 kB)
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

1 Reply

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

Answer by TBruce · Oct 04, 2016 at 09:02 PM

Try this approach

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class HudControl : MonoBehaviour
 {
     public Slider playerSlider;
     public Slider bossSlider;
 
     private int playerPrevHp, bossPrevHp;
 
     // Use this for initialization
     void Start ()
     {
         ResetValues();
     }
 
     void ResetValues ()
     {
         playerSlider.maxValue = 100;
         playerSlider.minValue = 0;
         playerSlider.value = 100;            // starts a 100 goes down to 0
 
         bossSlider.maxValue = 100;
         bossSlider.minValue = 0;
         bossSlider.value = 100;              // starts a 100 goes down to 0
 
         playerBf.hp = playerSlider.maxValue; // needs to start out at the same value as playerSlider.maxValue
         boss.bossHp = bossSlider.maxValue;   // needs to start out at the same value as bossSlider.maxValue
         
         playerPrevHp = 100;                  // needs to start out at the same value as playerSlider.maxValue
         bossPrevHp = 100;                    // needs to start out at the same value as bossSlider.maxValue
     }
 
     // Update is called once per frame
     void Update ()
     {
         if (playerBf.hp != playerPrevHp)
         {
             playerSlider.value = playerBf.hp;
             playerPrevHp = playerBf.hp;
         }
 
         if (boss.bossHp != bossPrevHp)
         {
             bossSlider.value = boss.bossHp;
             bossPrevHp = boss.bossHp;
         }
     }
 }

Please let me know if you have any questions

Comment
Add comment · Show 5 · 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 TBruce · Oct 05, 2016 at 05:54 PM 2
Share

@Andrekilik Did you find this answer helpful?

avatar image Andrekilik · Oct 05, 2016 at 06:38 PM 0
Share

Hi @$$anonymous$$avina, thanks for your suggestion, I've tried your approach, but the result was the same. It only works with one Slider on the scene. I even tried changing the objects, making the boss life bar as a Slide, and the player health bar as a image, so I would have two different kinds of objects, I expected that would work, but also didn't work; same results. If only Image object on scene, works perfectly. If only slider object on scene, works perfectly. the moment I insert another UI object in the scene, both stop working and do nothing, even when I'm seeing in the inspector that both the boss and the player are taking damage normally.

avatar image TBruce Andrekilik · Oct 05, 2016 at 07:33 PM 1
Share

Here is a Unity package with a demo that uses the above script (I had to create temporary PlayerBF and BossShotControl classes for the demo).

It is a simple demo that uses 2 sliders (white - player, red - boss) and 2 buttons (white - player, red - boss) that applies hit points to the respective sliders (tested and works).

Note: Please exa$$anonymous$$e the code and controls

Because you do not supply the script that changes the PlayerBF.hp and BossShotControl.bossHp values I am using functions in PlayerBF and BossShotControl that are called from the buttons when pressed (there is a public in each class called DecreaseHP()).

And here is a Youtube video of it in action.

avatar image Andrekilik TBruce · Oct 07, 2016 at 04:26 PM 0
Share

$$anonymous$$avina, thanks for the package and the support. I've opened your package and understood it. Then made a single change in my code. I've removed the Empty Game Object - HUD that I was using as a child of the canvas, and assigned the HUD-Control Script directly to the canvas, and now both sliders are working properly.

Show more comments

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

246 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 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

Is there drop event in slider? 0 Answers

Instantiate Objects in Pattern using UI Slider 1 Answer

Use a canvas prefab multiple times at the same time 0 Answers

Why does the canvas scale factor change when instantiating Prefab? 0 Answers

C# - Canvas UI addListener in code is not working 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