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:
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.
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
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.
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.
$$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.