Scoring points

Checked with version: 4.6

-

Difficulty: Beginner

This is part 8 of 10 of the Survival Shooter tutorial, in which you will program the player's score, updating it with each kill.

Scoring points

Beginner Survival Shooter tutorial

Transcripts

ScoreManager

Code snippet

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreManager : MonoBehaviour
{
    public static int score;        // The player's score.


    Text text;                      // Reference to the Text component.


    void Awake ()
    {
        // Set up the reference.
        text = GetComponent <Text> ();

        // Reset the score.
        score = 0;
    }


    void Update ()
    {
        // Set the displayed text to be the word "Score" followed by the score value.
        text.text = "Score: " + score;
    }
}
static var score : int;        // The player's score.

private var text : Text;       // Reference to the Text component.


function Awake ()
{
    // Set up the reference.
    text = GetComponent (Text);

    // Reset the score.
    score = 0;
}


function Update ()
{
    // Set the displayed text to be the word "Score" followed by the score value.
    text.text = "Score: " + score;
}

EnemyHealth

Code snippet

using UnityEngine;

public class EnemyHealth : MonoBehaviour
{
    public int startingHealth = 100;          // The amount of health the enemy starts the game with.
    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 isDead;                                  // Whether the enemy is dead.
    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;

        // 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 <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);
    }
}
import UnityEngine.UI;
var startingHealth : int = 100;          // The amount of health the enemy starts the game with.
var currentHealth : int;                    // The current health the enemy has.
var sinkSpeed : float = 2.5f;            // The speed at which the enemy sinks through the floor when dead.
var scoreValue : int = 10;                 // The amount added to the player's score when the enemy dies.
var deathClip : AudioClip;                 // The sound to play when the enemy dies.

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

function 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;
}

function 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 function TakeDamage (amount : int, hitPoint : Vector3)
{
    // 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 ();
    }
}

function Death ()
{
    // The enemy is dead.
    isDead = true;

    // Turn the collider into a trigger so shots can pass through it.
    capsuleCollider.isTrigger = true;

    // 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 function StartSinking ()
{
    // 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;

    // 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);
}

Related tutorials