- Home /
[help] enemy Health Bar decrease at the same time
Hi all~ i have problems on my enemy health bar, when i try to hit single enemy the health bar on all enemies will decrease at the same time, i don't know what happen in here. Can someone help me Fix this ?
here is my scripts Player attack :
var targets : GameObject [];
var attackTimer : float;
var coolDown : float;
var prefabBullet : Transform;
var shootForce : float;
var hand : Transform;
var fireRate : float = 1.4f;
public var playerTarget : Transform;
public var HealthEnabled:boolean = false;
public var lastPlayerTarget;
private var nextFire : float = 0f;
// Use this for initialization
function Start () {
targets = GameObject.FindGameObjectsWithTag("Enemy");
attackTimer = 0;
coolDown = 1.0f;
}
// Update is called once per frame
function Update ()
{
if(Health == 0)
{
PlayerAnimation.Stop("dead");
}
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.Mouse0) || Input.GetKey (KeyCode.Return) && prefabBullet.rigidbody)
{
if(attackTimer == 0)
{
if( Health.health > 0 && EnemyHealthJava2.healthBarLength > 0)
{
//Health.health > 5;
Attack();
attackTimer = coolDown;
}
}
}
}
function Attack()
{
for (var i = 0; i < targets.Length; i++)
{
var distance : float = Vector3.Distance(targets[i].transform.position, transform.position);
var dir : Vector3 = (targets[i].transform.position - transform.position).normalized;
var direction : float = Vector3.Dot(dir, transform.forward);
if(distance < 10)
{
if(direction > 0)
{
nextFire = Time.time + fireRate;
var clone : Transform = Instantiate(prefabBullet, hand.position, hand.rotation) as Transform;
clone.rigidbody.velocity = transform.TransformDirection(Vector3.forward * 20);
animation.Play("shoot");
var eh : EnemyHealthJava2 = targets[i].GetComponent("EnemyHealthJava2");
eh.AdjustCurrentHealth(-10);
}
}
}
}
here is the enemy health:
var playerName : String;
var adjustment : float= 2.3f;
var healthTex : Texture2D;
static var health : int = 100;
static var curHealth : int = 100;
static var enemy : String;
static var alive : boolean = true;
static var healthBarLength : float = 100;
private var worldPosition : Vector3= new Vector3();
private var screenPosition : Vector3= new Vector3();
private var myTransform : Transform;
private var myCamera : Camera;
private var healthBarHeight : int= 5;
private var healthBarLeft : int= 110;
private var barTop : int= 1;
//private var healthBarLength : float= 100;
private var labelTop : int= 18;
private var labelWidth : int= 110;
private var labelHeight : int= 15;
private var myStyle : GUIStyle= new GUIStyle();
function Awake ()
{
myTransform = transform;
myCamera = Camera.main;
myStyle.normal.textColor = Color.green;
myStyle.fontSize = 12;
myStyle.fontStyle = FontStyle.Bold;
myStyle.clipping = TextClipping.Overflow;
}
function Update () {
if (healthBarLength != null && curHealth <= 0)
{
Destroy(gameObject);
Debug.Log("Enemy Death");
return;
}
}
function OnGUI ()
{
worldPosition = new Vector3(myTransform.position.x, myTransform.position.y + adjustment,myTransform.position.z);
screenPosition = myCamera.WorldToScreenPoint(worldPosition);
GUI.Box(new Rect(screenPosition.x - healthBarLeft / 2, Screen.height - screenPosition.y - barTop,100, healthBarHeight), "");
GUI.DrawTexture(new Rect(screenPosition.x - healthBarLeft / 2, Screen.height - screenPosition.y - barTop,healthBarLength, healthBarHeight), healthTex);
GUI.Label(new Rect(screenPosition.x - labelWidth / 2, Screen.height - screenPosition.y - labelTop, labelWidth, labelHeight), playerName, myStyle);
}
static function UpOrDown (amount : int)
{
healthBarLength += amount;
}
public function AdjustCurrentHealth( adj : int)
{
healthBarLength += adj;
if(healthBarLength <=0 && alive)
{
alive = false;
healthBarLength = 0;
}
if(healthBarLength > health)
healthBarLength = health;
if(health < 1)
health = 1;
}
Answer by LyanApps · Apr 09, 2013 at 02:13 AM
You don't want to use the keyword static in static var health : int = 100;
That means one instance of this variable for all instances of your class.
Answer by Apriyanto · Apr 09, 2013 at 06:52 AM
SOLVED problems , can see in here http://forum.unity3d.com/threads/177470-help-enemy-Health-Bar-decrease-at-the-same-time?p=1214131&posted=1#post1214131
Your answer
![](https://koobas.hobune.stream/wayback/20220613095931im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My healtbar is not change when i look another enemy 0 Answers
C# Floating Health Bar 2 Answers
Best way to make a health par over enemies 2 Answers
How can I do health bar follows enemy? 1 Answer
Enemy HealthBar Problems 2 Answers