2017-03-06 7 views
1

私はUnity3Dを初めて使いました。私は実行時に殺したすべての敵を数えようとしており、OnGUI()に表示しています。私がしたことは、敵が殺されるたびに増分することです:enemyKilled。しかし、プログラムの中で何度か...ディスプレイが減少してから消えます。Unity3Dで殺された敵を表示してカウントするC#

public class ShootableBox : MonoBehaviour { 

    Animator anim; 
    public int currentHealth = 3;  
    public int enemyKilled;   // Count how many enemies you have killed   
    public float currentHealthLength; 

    bool isSinking;      // To trigger the dead enemy to sink 
    public float sinkSpeed = 1f;  // Sink speed 

    void Start() { 
     anim = GetComponent<Animator>(); 
     currentHealthLength = Screen.width/2; 
     enemyKilled = 0; 
    } 

    void Update() 
    { 
     // If the enemy should be sinking... 
     if(isSinking) 
     { 
      transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime); 
     }  
    } 


    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) { 
      enemyKilled = enemyKilled + 1; 
      //if health has fallen below zero, deactivate it 
      anim.SetTrigger ("isDead"); 
      //transform.GetComponent<NavMeshAgent>().Stop(); 

      // Find and disable the Nav Mesh Agent. 
      GetComponent <NavMeshAgent>().enabled = false; 

      // Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy). 
      GetComponent <Rigidbody>().isKinematic = true; 

      Invoke ("StartSinking", 2.5f); 

     } else { 
      anim.SetTrigger("isHit"); 
      anim.SetTrigger("isRun2"); 
     } 
    } 

    public void StartSinking() { 

     // The enemy should now sink. 
     isSinking = true; 
     // After 2 seconds destory the enemy. 
     Destroy (gameObject, 8f); 
    } 

    // My problem is somewhere on this part 
    public void OnGUI() { 

     GUI.contentColor = Color.yellow; 

     if (enemyKilled != 0) { 

      GUI.Box(new Rect(5, 5, currentHealthLength, 20), "Enemies killed: " + enemyKilled); 
     } 
    } 
} 

実行時に既に敵を殺すことができます。私はちょうど正しい数の死んだ敵を表示するようには思えません。このエラーを修正するにはどうすればよいですか?すべてのあなたの助けをありがとうございました。

+0

このスクリプトはあなたの敵に付けられていますか? (またはこの場合は撃ち可能な箱) –

+0

@ Mr.Bigglesworthはい、持っています。 –

+0

あなたはこのスクリプトをプレイヤーまたは敵につないでいますか?これは大きな違いになります。 –

答えて

2

このスクリプトはすべての敵にありますので、各敵に異なるスコアが与えられ、それをGUIに出力しようとします。

実際には、StartSinking関数でオブジェクトを実際に破棄しますが、GUIを表示して一度破棄するようにします。

スコアを保持するスクリプトは、破棄できないGameManagerクラスの何らかの形である必要があります。正確なスコアを保持するので、1つだけでなければなりません。

たとえば、GameManagerというクラスがあり、シーン内のゲームオブジェクトにアタッチされています。どんな種類のgameobjectが空でもかまいません。スコアを保持して画面に表示するクラスです。

敵のスクリプトは、敵が何をしているのかを制御し、GUIの呼び出しを取り除くだけですべきです。敵が死亡すると、GameManagerクラスを呼び出し、別の敵が死亡したことを伝え、スコアを更新することができます。

public class GameManager : MonoBehaviour{ 

private int enemyKilled= 0; 

    public void UpdateScore() 
    { 
    enemyKilled++; 
    } 

    public void OnGUI() 
    { 
    GUI.contentColor = Color.yellow; 
    GUI.Box(new Rect(5, 5, 20, 20), "Enemies killed: " + enemyKilled); 
    } 
} 

上記は、UpdateScoreメソッドが呼び出されるたびにスコアを上げるマネージャの例です。次に、スコアを表示するOnGui関数があります。

すべての敵のニーズは、メソッドを呼び出すことができるように、GameManagerスクリプトへの参照です。これは簡単で、いくつかの方法があります。 GameManagerタイプの変数を作成し、そのシーンを検索するか、それよりも優れていますが、常にアクセスできる静的なGameManager(シングルトンメソッド)を作成することを検討してください。

希望に役立ちます。

+0

これは役に立ちますか、それとも意味がないのですか?説明したり、例を提供したりして嬉しいです。 –

+0

私はそれを理解しようとしています。例を挙げても大丈夫でしょうか?どうもありがとうございます。 –

+0

はい答えを更新します –

関連する問題