以下脚本会对我的敌人预制件造成伤害,但其上方的健康栏与其当前的健康状况不符。它不会对其各自的健康栏进行更改,而只会更改层次结构窗口中预制件的第一个实例。但是,当对敌人施加正确的伤害时,敌人仍会被摧毁。需要注意的是健康栏图像是画布的子画面,画布是主预制件的子画面,并附有脚本。我该如何解决这个问题?
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
private float curHealth = 100f;
private float GunDamage;
private GameObject hb;
private GameObject gd;
private GameObject canvas;
public float maxHealth;
public string gunType;
void Start () {
canvas = GameObject.Find ("HealthCanvas");
hb = GameObject.Find ("HealthBar");
gd = GameObject.Find (gunType);
GunDamage = gd.GetComponent<Shoot> ().bulletDamage;
}
public void SubtractHealth(float howMuch){
curHealth = curHealth - howMuch;
if (curHealth <= 0) {
Destroy (this.gameObject);
}
//Update health bar
hb.GetComponent<RectTransform>().sizeDelta = new Vector2 (hb.GetComponent<RectTransform>().sizeDelta.x - 0.95f / maxHealth * GunDamage , hb.GetComponent<RectTransform>().sizeDelta.y);
Debug.Log (curHealth);
}
}
答案 0 :(得分:1)
canvas = this.transform.FindChild ("HealthCanvas").gameObject;
hb = canvas.transform.FindChild ("HealthBar").gameObject;