- Home /
Enemy Health Problem
Hello! I would like to ask a basic question about enemy health. I wrote a basic enemy health script for enemies and attack script for player. I wrote if enemy health = 0 destroy the game object . Here is the problem when the enemy health = 0, all enemies are died and destroyed. how can I add new health for each enemy? How can I fixed this? thank you!
If you have an enemy specific script(one script attached to every enemy) and heave a non static health it should work. As robertbu said, seeing your script would make it easier to help.
Answer by rocknrolla33 · Nov 20, 2013 at 02:03 AM
I found the problem, like you said because of the static variable I fixed this. thank you. But here another problem how can I match Selected Target's health from Player attack script. I can't understand the logic I think.
here the scripts
using UnityEngine; using System.Collections;
public class EnemyHealth : MonoBehaviour {
     public int maxHealth = 100;
 public int curHealth = 100;
 public float healthBarLength;
 // Use this for initialization
 void Start () {
     healthBarLength = Screen.width / 2;
 
 }
 
 // Update is called once per frame
 void Update () {
     AddjustCurrentHealth(0);
     if(curHealth <= 0)
         Destroy(gameObject);
 }
 
 void OnGUI(){
     GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth + "/" + maxHealth);
 }
 
 public void AddjustCurrentHealth(int adj){
     
 
         
     curHealth += adj;
     
     if(curHealth <0)
         curHealth = 0;
     
     if(curHealth > maxHealth)
         curHealth = maxHealth;
     
     if(maxHealth < 1)
         maxHealth = 1;
     
     healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
     
 
 }
}
public class PlayerAttack : MonoBehaviour {
// public GameObject target;
public float attackTimer; public float coolDown;
 // Use this for initialization
 void Start () {
     attackTimer = 0;
     coolDown = 2.0f;
         
 }
 
 // Update is called once per frame
 void Update () {
     
     if(attackTimer > 0)
         attackTimer -= Time.deltaTime;
     
     if(attackTimer <0)
         attackTimer = 0;
     
     
     if(Input.GetKeyUp(KeyCode.F)){    
         if(attackTimer == 0){
             Attack();
         
         attackTimer = coolDown;
         }
     }
 }
 private void Attack(){
     
     
// float distance = Vector3.Distance(target.transform.position, transform.position);
// Vector3 dir = (target.transform.position - transform.position).normalized;
// float direction = Vector3.Dot(dir,transform.forward);
// Debug.Log (direction);
     if(distance < 2.5f){
         if(direction > 0){
     
     EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
     eh.AddjustCurrentHealth(-40);    
             
             
         }
 
     }
 }
 
}
Blockquote
/// /// TargettingMob.cs /// /// This script can be attached to any permanent gameobject, and is responsible for allowing the player to target different mobs that are with in range ///
using UnityEngine; using System.Collections;
// List komutunu kullana bilmek için alttaki komut gerekli using System.Collections.Generic;
public class TargettingMob : MonoBehaviour {
 public List<Transform> targets;
public Transform selectedTarget; private Transform myTransform;
 // Use this for initialization
 void Start () {
 targets = new List<Transform>();
     selectedTarget = null;
     myTransform = transform;
     
         
     
     AddAllEnemies();
 }
 
 public void AddAllEnemies()
 {
     GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
     
     foreach(GameObject enemy in go)
         AddTarget(enemy.transform);
 }
 
 
 public void AddTarget(Transform enemy){
     targets.Add(enemy);
     
 }
 
 private void SortTargetByDistance()
 {
     targets.Sort(delegate(Transform t1, Transform t2){ 
         return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
             
             });
     
     
 }
 
 private void TargetEnemy()
 
 {
     if(selectedTarget == null)
     {
         SortTargetByDistance();
     selectedTarget = targets[0];
         
     }
     else
     {
         int index = targets.IndexOf(selectedTarget);
         
         if(index < targets.Count -1)
         {
             index++;
             
         }
         else
         {
             index = 0; 
             
         }
         DeselectTarget();
         selectedTarget = targets[index];
                     
     }
     SelectedTarget();
         
 }
 
 private void SelectedTarget()
 {    
     Transform name = selectedTarget.FindChild("Name");
     
     if(name == null){
         Debug.LogError("Could not find the Name on " + selectedTarget.name);
         return;
     }
     
     name.GetComponent<TextMesh>().text = selectedTarget.GetComponent<Mob>().Name;
     name.GetComponent<MeshRenderer>().enabled = true;
             
     
 
// selectedTarget.GetComponent().DisplayHealth();
// Messenger.Broadcast("show mob vitalbars", true);
 }
 
 private void DeselectTarget()
 {    
     selectedTarget.FindChild("Name").GetComponent<MeshRenderer>().enabled = false;
     
     selectedTarget = null;
// Messenger.Broadcast("show mob vitalbars", false);
 }
 
 // Update is called once per frame
 void Update () {
     if(Input.GetKeyDown(KeyCode.Tab))
     {
         TargetEnemy();
         
         
 }
 
 }
}
Your answer
 
 
             Follow this Question
Related Questions
Making enemy push player 1 Answer
Hurt Player if close to Enemy! Need Help 4 Answers
Make Enemy Solid Object 2 Answers
Enemy healthbar script 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                