2013-02-25 48 views
0

我在Unity3D中用C#製作遊戲。我正在使用GUI.box爲小怪顯示一個健康欄,但如果有目標,我只想顯示GUI.box。Unity3D:如果條件爲真,如何顯示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); 
} 

}

+2

仔細檢查你的盒子的位置參數。我猜你是在告訴它不在屏幕上,或者是長度。 – 2013-02-26 00:06:06

+1

我也在考慮位置參數是這裏的問題,試着用一個更大的(200x50)在原點來看看會發生什麼。順便說一下,在OnGUI()中檢查'target!= null'的情況不是更容易嗎?請記住,這種方法可能每幀被調用一次以上。 – 2013-02-26 00:16:46

+0

感謝您的評論! 我不認爲這是位置參數,因爲GUI.box顯示完全正常,如果我只是帶走if條件。起初,我確定GUI.box顯示正常,但隨後我一直看到所有小怪的生命線,這當然不是很好 - 我的第一次嘗試是在OnGUI()中有條件,但沒有奏效,所以我認爲它不會在每一幀更新,所以我將它移動到Update()。現在再試一次,但沒有奏效。 – Lennev 2013-02-26 00:47:33

回答

1

你永遠將目標設置爲Start()方法以外的任何位置?如果PlayerAttack.Target在開始時不爲空,則您顯示的代碼將只顯示GUI.box。嘗試將此代碼移至Update()方法。

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

編輯:看到

檢查目標爲空,這可能是問題。

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

這將打印到日誌中,無論它是什麼遊戲對象,或null。如果它是空的,那麼就沒有目標。

+0

感謝您的提示,不幸的是它沒有工作,但:/ – Lennev 2013-02-26 07:56:21

+0

是附加到您試圖從中獲得它從相同的組件的PlayerAttack腳本? – Heisenbug 2013-02-26 08:45:54

+0

不,它不是。只是附加它,但是這也不起作用:( – Lennev 2013-02-26 10:14:42