deathCounter 不递增

deathCounter not incrementing

您好,在我尝试修改的这款游戏中,有 3 种不同类型的敌人,它们都具有相同的 enemyHealth 脚本。在这个健康脚本中,我试图在每次敌人死亡时增加一个计数器。下面的代码显示了 enemyHealth class

using UnityEngine;

namespace CompleteProject
{
    public class EnemyHealth : MonoBehaviour
    {
        public int startingHealth = 100;            // The amount of health the enemy starts the game with.
        public bool isDead;                                // Whether the enemy is dead.
        public int deathCount;
        public int currentHealth;                   // The current health the enemy has.
        public float sinkSpeed = 2.5f;              // The speed at which the enemy sinks through the floor when dead.
        public int scoreValue = 10;                 // The amount added to the player's score when the enemy dies.
        public AudioClip deathClip;                 // The sound to play when the enemy dies.


        Animator anim;                              // Reference to the animator.
        AudioSource enemyAudio;                     // Reference to the audio source.
        ParticleSystem hitParticles;                // Reference to the particle system that plays when the enemy is damaged.
        CapsuleCollider capsuleCollider;            // Reference to the capsule collider.
        bool isSinking;                             // Whether the enemy has started sinking through the floor.


        void Awake ()
        {
            // Setting up the references.
            anim = GetComponent <Animator> ();
            enemyAudio = GetComponent <AudioSource> ();
            hitParticles = GetComponentInChildren <ParticleSystem> ();
            capsuleCollider = GetComponent <CapsuleCollider> ();

            // Setting the current health when the enemy first spawns.
            currentHealth = startingHealth;
        }


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

            }
        }


        public void TakeDamage (int amount, Vector3 hitPoint)
        {
            // If the enemy is dead...
            if(isDead)
                // ... no need to take damage so exit the function.
                return;

            // Play the hurt sound effect.
            enemyAudio.Play ();

            // Reduce the current health by the amount of damage sustained.
            currentHealth -= amount;

            // Set the position of the particle system to where the hit was sustained.
            hitParticles.transform.position = hitPoint;

            // And play the particles.
            hitParticles.Play();

            // If the current health is less than or equal to zero...
            if(currentHealth <= 0)
            {
                // ... the enemy is dead.
                Death ();
            }
        }


        void Death ()
        {
            // The enemy is dead.
            isDead = true;
            // Turn the collider into a trigger so shots can pass through it.
            capsuleCollider.isTrigger = true;
            deathCount++;
            Debug.Log("Deaths" + deathCount);

            // Tell the animator that the enemy is dead.
            anim.SetTrigger ("Dead");

            // Change the audio clip of the audio source to the death clip and play it (this will stop the hurt clip playing).
            enemyAudio.clip = deathClip;
            enemyAudio.Play ();
        }


        public void StartSinking ()
        {
            // Find and disable the Nav Mesh Agent.
            GetComponent <UnityEngine.AI.NavMeshAgent> ().enabled = false;

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

            // The enemy should no sink.
            isSinking = true;

            // Increase the score by the enemy's score value.
            ScoreManager.score += scoreValue;
            // After 2 seconds destory the enemy.
            Destroy (gameObject, 2f);

        }
    }
}

游戏中的所有 3 个敌人都在其预制件上附加了此脚本,因此它们的行为方式相同,所以我不明白为什么当敌人被摧毁时计数器不会增加。

调试时计数器似乎变为 1,然后每次击杀之后都再次保持在 1。

没错。它是一个成员,所以它会存在于你的敌人的每个实例中。您想跟踪三种不同的健康状况,对吗?

所以敌人 1 开始时是 100,敌人 2 是 100,敌人 3 是 100。当敌人 1 受到 20 伤害时,它会变成 80 而 2,3 保持在 100,对吗?为什么柜台的行为会有所不同呢?都是从0开始,死后到1。

要么有一个上层对象(例如 enemymanager)并计算敌人死亡的次数(首选解决方案),要么使用静态变量。