【问题标题】:Health bars that work independently独立工作的健康棒
【发布时间】:2016-09-21 20:27:40
【问题描述】:

以下脚本对我的敌人预制件造成伤害,但其上方的生命值条与其当前的生命值不匹配。它不会更改其各自的健康栏,而是仅更改层次结构窗口中预制件的第一个实例。但是,当对他们施加正确的伤害时,敌人仍然会被摧毁。需要注意的是,健康栏图像是画布的子对象,而画布是主预制件的子对象,并附有脚本。我该如何解决这个问题?

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);
    }
}

【问题讨论】:

    标签: c# unity3d monodevelop


    【解决方案1】:
    canvas = this.transform.FindChild ("HealthCanvas").gameObject;
    hb = canvas.transform.FindChild ("HealthBar").gameObject;
    

    【讨论】:

      猜你喜欢
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      • 2013-12-31
      • 2020-07-26
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多