Search Unity

Survival Shooter enemies wont take damage

Discussion in 'Game Design' started by ryan_unity21, Feb 21, 2019.

  1. ryan_unity21

    ryan_unity21

    Joined:
    Jan 17, 2019
    Posts:
    10
    I'm doing the survival shooter tutorial for unity and I just added the script which makes the enemies take damage from the players gun. I added the script to the enemy but whenever I play it I get 2 errors. Every time I insert a script on here using Ctrl + k it never works so here's a link to it

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EnemyDamage : MonoBehaviour
    4. {
    5.     public int startingHealth = 100;            // The amount of health the enemy starts the game with.
    6.     public int currentHealth;                   // The current health the enemy has.
    7.     public float sinkSpeed = 2.5f;              // The speed at which the enemy sinks through the floor when dead.
    8.     public int scoreValue = 10;                 // The amount added to the player's score when the enemy dies.
    9.     public AudioClip deathClip;                 // The sound to play when the enemy dies.
    10.  
    11.  
    12.     Animator anim;                              // Reference to the animator.
    13.     AudioSource enemyAudio;                     // Reference to the audio source.
    14.     ParticleSystem hitParticles;                // Reference to the particle system that plays when the enemy is damaged.
    15.     CapsuleCollider capsuleCollider;            // Reference to the capsule collider.
    16.     bool isDead;                                // Whether the enemy is dead.
    17.     bool isSinking;                             // Whether the enemy has started sinking through the floor.
    18.  
    19.  
    20.     void Awake()
    21.     {
    22.         // Setting up the references.
    23.         anim = GetComponent<Animator>();
    24.         enemyAudio = GetComponent<AudioSource>();
    25.         hitParticles = GetComponentInChildren<ParticleSystem>();
    26.         capsuleCollider = GetComponent<CapsuleCollider>();
    27.  
    28.         // Setting the current health when the enemy first spawns.
    29.         currentHealth = startingHealth;
    30.     }
    31.  
    32.     void Update()
    33.     {
    34.         // If the enemy should be sinking...
    35.         if (isSinking)
    36.         {
    37.             // ... move the enemy down by the sinkSpeed per second.
    38.             transform.Translate(-Vector3.up * sinkSpeed * Time.deltaTime);
    39.         }
    40.     }
    41.  
    42.  
    43.     public void TakeDamage(int amount, Vector3 hitPoint)
    44.     {
    45.         // If the enemy is dead...
    46.         if (isDead)
    47.             // ... no need to take damage so exit the function.
    48.             return;
    49.  
    50.         // Play the hurt sound effect.
    51.         enemyAudio.Play();
    52.  
    53.         // Reduce the current health by the amount of damage sustained.
    54.         currentHealth -= amount;
    55.  
    56.         // Set the position of the particle system to where the hit was sustained.
    57.         hitParticles.transform.position = hitPoint;
    58.  
    59.         // And play the particles.
    60.         hitParticles.Play();
    61.  
    62.         // If the current health is less than or equal to zero...
    63.         if (currentHealth <= 0)
    64.         {
    65.             // ... the enemy is dead.
    66.             Death();
    67.         }
    68.     }
    69.  
    70.  
    71.     void Death()
    72.     {
    73.         // The enemy is dead.
    74.         isDead = true;
    75.  
    76.         // Turn the collider into a trigger so shots can pass through it.
    77.         capsuleCollider.isTrigger = true;
    78.  
    79.         // Tell the animator that the enemy is dead.
    80.         anim.SetTrigger("Dead");
    81.  
    82.         // Change the audio clip of the audio source to the death clip and play it (this will stop the hurt clip playing).
    83.         enemyAudio.clip = deathClip;
    84.         enemyAudio.Play();
    85.     }
    86.  
    87.  
    88.     public void StartSinking()
    89.     {
    90.         // Find and disable the Nav Mesh Agent.
    91.         GetComponent<NavMeshAgent>().enabled = false;
    92.  
    93.         // Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy).
    94.         GetComponent<Rigidbody>().isKinematic = true;
    95.  
    96.         // The enemy should no sink.
    97.         isSinking = true;
    98.  
    99.         // Increase the score by the enemy's score value.
    100.         ScoreManager.score += scoreValue;
    101.  
    102.         // After 2 seconds destory the enemy.
    103.         Destroy(gameObject, 2f);
    104.     }
    105. }
    The 2 errors are

    Assets/EnemyDamage.cs(91,22): error CS0246: The type or namespace name NavMeshAgent' could not be found. Are you missingUnityEngine.AI' using directive?

    Assets/EnemyDamage.cs(91,38): error CS1061: Type T' does not contain a definition forenabled' and no extension method enabled' of typeT' could be found. Are you missing an assembly reference?

    I've looked for answers but no one seems to be having the same problem.

    EDIT

    I added

    using UnityEngine.AI;

    and now it dosent give me errors, but they wont take damage. No clue what to do I feel like it has to do something with this message I get every time I play the game

    The referenced script on this Behaviour (Game Object 'GunBarrelEnd') is missing!

    but I dont have any referenced scripts in it
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Wrong forum for these types of questions. Add debugging to TakeDamage to see if it is being called at all. Also, do more investigation on the missing referenced script, as you're probably checking the wrong gameobject.