problems with slider bar,Problem with slider bar
So i have two towers with a health bar. The problem is, that one is working perfectly, but the other gets attached to the other health bar and not its own. I attached its slider, but when i hit play it changes. Here is the code fot each health bar:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class HealthBar : MonoBehaviour { [SerializeField] private Slider slider; public float maxHealth; public float currentHealth; [SerializeField] private Rigidbody2D myBody; [SerializeField] private SpriteRenderer sr; public float attackInterval = 1.0f;
 void Awake()
 {
     sr = GetComponent<SpriteRenderer>();
     myBody = GetComponent<Rigidbody2D>();
     if (GameObject.FindGameObjectWithTag("healthSlider"))
     {
         slider = (Slider)FindObjectOfType(typeof(Slider));
     }
 }
 void start()
 {
     slider.value = maxHealth;
 }
 void update()
 {
     slider.value = currentHealth;
 }
 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.CompareTag("Warrior"))
     {
         StartCoroutine(attack());
         TakeDamage(5);
     }
 }
 void TakeDamage(float damage)
 {
     currentHealth -= damage;
     slider.value = currentHealth;
 }
 IEnumerator attack()
 {
     yield return new WaitForSeconds(attackInterval);  //wait attack interval
     TakeDamage(5);
     StartCoroutine(attack());  //attack again
 }
} //this is the one working
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class HealthBarNave : MonoBehaviour
{ [SerializeField] private Slider sliderNave; public float maxHealth; public float currentHealth; [SerializeField] private Rigidbody2D myBody; [SerializeField] private SpriteRenderer sr; public float attackInterval = 1.0f;
 void Awake()
 {
     sr = GetComponent<SpriteRenderer>();
     myBody = GetComponent<Rigidbody2D>();
     if (GameObject.FindGameObjectWithTag("healthSliderNave"))
     {
       sliderNave = (Slider)FindObjectOfType(typeof(Slider));
     }
 }
 void start()
 {
     sliderNave.value = maxHealth;
     /* if (GameObject.FindGameObjectWithTag("healthSliderNave"));
     {
       sliderNave = (Slider)FindObjectOfType(typeof(Slider));
     }*/
 }
 void update()
 {
     sliderNave.value = currentHealth;
 }
 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.CompareTag("Enemy"))
     {
         StartCoroutine(attack());
         TakeDamage(5);
     }
 }
 void TakeDamage(float damage)
 {
     currentHealth -= damage;
     sliderNave.value = currentHealth;
 }
 IEnumerator attack()
 {
     yield return new WaitForSeconds(attackInterval);  //wait attack interval
     TakeDamage(5);
     StartCoroutine(attack());  //attack again
 }
} //and this one doesnt work
,
Your answer
 
 
             Follow this Question
Related Questions
How to sync health bar with Photon Unity Network 0 Answers
"Clipping", "Jumping" UI slider, attached to the 2d game object ( health bar) 0 Answers
How do I decrease a slider over time? 1 Answer
"Clipping", "Jumping" UI slider, attached to the 2d game object ( health bar) 0 Answers
Healthbar/UI Slider glow effect 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                