Unity3D:如果条件为真,如何显示GUI.box



我正在用C#在Unity3D中制作一个游戏。我使用GUI.box为暴徒显示健康条,但我只想在有目标的情况下显示GUI.box。

这是我现在的密码。

public GameObject target;
public bool existsTarget;
// Use this for initialization
void Start()
{
PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
target = pa.target;
existsTarget = false;
}
// Update is called once per frame
void Update()
{
if(target != null)
existsTarget = true;
else
existsTarget = false;

}
void OnGUI()
{
if(existsTarget)
        GUI.Box(new Rect(500, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
    else   {
       GUI.Box(new Rect(Screen.width, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
   }

不幸的是,这根本没有显示任何健康条。有什么原因吗?

根据流行需求在这里发布脚本。

public class Targetting : MonoBehaviour {
public List<Transform> targets;
public List<Transform> items;
public GameObject TheSelectedTarget {get; set;}
private Transform selectedTarget;
private Transform selectedItem;
private Transform myTransform;
// Use this for initialization
void Start () {
    targets = new List<Transform>();
    items = new List<Transform>();
    selectedTarget = null;
    selectedItem = null;
    myTransform = transform;
    TheSelectedTarget = null;
    addAllEnemies();
    addAllItems();
}
//adds all targets to a list
private void addAllEnemies() {
    GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
    foreach(GameObject enemy in go){
        addTarget(enemy.transform);
    }
}
//adds a target
private void addTarget(Transform enemy) {
    targets.Add(enemy);
}
//sorts target by distance
private void sortTargets() {
    targets.Sort(delegate(Transform t1, Transform t2) {
        return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
    });
}
//targets an enemy
private void targetEnemy() {
    addAllEnemies();
    if(selectedTarget == null) {
        sortTargets();
        selectedTarget = targets[0];
    } else {
        int index = targets.IndexOf(selectedTarget);
        if(index < targets.Count -1) {
            index++;
        } else {
            index = 0;
        } 
        deselectTarget();
        selectedTarget = targets[index];
}
    selectTarget();
    targets.Clear();
    }
//selects a specific target, and colors it red
public void selectTarget() {
    selectedTarget.renderer.material.color = Color.red;
    PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
    pa.target = selectedTarget.gameObject;
    TheSelectedTarget = pa.target;
}
//deselects the current selected target, and colors i grey
private void deselectTarget() {
    selectedTarget.renderer.material.color = Color.gray;
    selectedTarget = null;
}
//adds all items to a list
void addAllItems() {
GameObject[] go = GameObject.FindGameObjectsWithTag("Book");
foreach(GameObject book in go){
    addItem(book.transform);
}

}

'

然后剧本继续,但与此无关。。。

'          public class EnemyHealth : MonoBehaviour
          {
            public string enemyName;
              public int maxHealth = 100;
              public int curHealth = 100;
              public float healthBarLength;
              public GameObject target;
          public bool existsTarget;
          public AudioSource dying;
    // Use this for initialization
    void Start()
{
    //enemyName = this.enemyName;
    healthBarLength = Screen.width / 3;
    existsTarget = false;
}
// Update is called once per frame
void Update()
{
    adjustCurHealth(0);
    Targetting ta = (Targetting)GetComponent("Targetting");
    target = ta.TheSelectedTarget;
    Debug.Log (target);
    if(target != null)
        existsTarget = true;
    else {
        existsTarget = false;   
    }
}
void OnGUI()
{
    if(existsTarget)
        GUI.Box(new Rect(500, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
    else   {
       GUI.Box(new Rect(Screen.width, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
   }
}
public void adjustCurHealth(int adj)
{
    curHealth += adj;
    if (curHealth < 0)
        curHealth = 0;
    if (curHealth > 100)
        curHealth = 100;
    if (maxHealth < 0)
        maxHealth = 1;
    if(curHealth == 0)
    {
        //dying.Play ();
        GameObject.Destroy(gameObject);
    }
    healthBarLength = (Screen.width / 3) * (curHealth / (float)maxHealth);
}

}

除了Start((方法之外,您还设置过目标吗?只有当PlayerAttack.Target在开始时不为空时,您显示的代码才会显示GUI.box。尝试将此代码移动到Update((方法。

PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
target = pa.target;

编辑:

检查目标是否为null,这可能是问题所在。

target = pa.target;
Debug.Log(target);

这将以任何GameObject的形式打印到日志中,或者为null。如果它为null,那么就没有目标。

相关内容

  • 没有找到相关文章

最新更新