我在這里用上一個問題解決了這個問題,但現在它仍然無法正常作業。幸運的是,我想我已經解決了這個問題。它沒有檢測到敵人已被殺死/摧毀。
我設定代碼的方式是,當敵人失去健康時,物件會被停用。但我認為它會檢測到物件的實體何時被停用。但顯然不是(至少從我所見)。
我認為最好的做法是讓我的敵人的“死亡腳本”以某種方式與我的獲勝條件聯系起來(除非這不是正確的做法,在這種情況下讓我知道正確的行動是什么)。
我認為這將使敵人變數成為全域變數,但我不確定如何做到這一點,或者它是否可以肯定地作業。
重申一下,我對 C# 和統一是完全陌生的,所以如果它的解決方案很明顯或者問題只是愚蠢的,請記住我不知道從這一點開始(這就是為什么首先創建這個問題地方)。如果存在可以解決此問題的現有問題,請鏈接它,否則,任何幫助。
這是獲勝條件的代碼
public class GameWinner : MonoBehaviour
{
public GameObject[] enemies;
public Font font;
void OnTriggerEnter(Collider other)
{
enemies = GameObject.FindGameObjectsWithTag("Enemy"); // Checks if enemies are available with tag "Enemy". Note that you should set this to your enemies in the inspector.
if (enemies == null)
{
SceneManager.LoadScene(2);
}
}
}
這是摧毀敵人的代碼(注意,這是從教程中摘錄的,特別是“讓我們試試射手”,讓我知道完全重做它是否有益)
public class ShootableBox : MonoBehaviour
{
//The box's current health point total
public int currentHealth = 3;
public void Damage(int damageAmount)
{
//subtract damage amount when Damage function is called
currentHealth -= damageAmount;
//Check if health has fallen below zero
if (currentHealth <= 0)
{
//if health has fallen below zero, deactivate it
gameObject.SetActive (false);
}
}
}
uj5u.com熱心網友回復:
空陣列和空參考之間存在差異。
在您的情況下,您應該檢查陣列是否為空:
enemies = GameObject.FindGameObjectsWithTag("Enemy"); // Checks if enemies are available with tag "Enemy". Note that you should set this to your enemies in the inspector.
if (enemies.Length == 0)
SceneManager.LoadScene(2);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/528957.html
標籤:C#unity3d
