Player Death leads to game end

Hello I followed a video tutorial that works with navmesh But the game doesn’t end when the player reaches 0 health
The video is called Enemies Made Easy (Unity3D + NavMesh) by Developer Jake

Here is the enemy code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;


public class Enemy : MonoBehaviour
{
   [Range(0,50)] [SerializeField] float attackRange = 5, sightRange = 20 , timeBetweenAttacks = 3;

    [Range(0, 20)][SerializeField] int power; // The amount of damge that the enemy does

    private NavMeshAgent thisEnemy;
    public Transform playerPos;

    private bool isAttacking; // If the enemy is currently attacking

    private void Start()
    {
        thisEnemy = GetComponent<NavMeshAgent>();
        playerPos = FindObjectOfType<PlayerHealth>().transform;
    }

    private void Update()
    {
        float distanceFromPlayer = Vector3.Distance(playerPos.position, this.transform.position); //The distance between the Player and the Enemy

        if (distanceFromPlayer <= sightRange && distanceFromPlayer > attackRange && !PlayerHealth.isDead)
        {
            isAttacking = false;
            thisEnemy.isStopped = false;
            StopAllCoroutines();

            ChasePlayer();
        }

        if (distanceFromPlayer <= attackRange && !isAttacking && !PlayerHealth.isDead)
        {
            thisEnemy.isStopped = true; // Stop the Enemy from moving
            StartCoroutine(AttackPlayer());// Start attacking the player
        }

        if (PlayerHealth.isDead)
        {
            thisEnemy.isStopped = true;
        }
    }

    private void ChasePlayer()
    {
        thisEnemy.SetDestination(playerPos.position); // Set the enemy's destination to the player.
    }

    private IEnumerator AttackPlayer()
    {
        isAttacking = true;

        yield return new WaitForSeconds(timeBetweenAttacks); // Wait for the time between attacks

        FindObjectOfType<PlayerHealth>().TakeDamge(power); // Damage the player with 'power' damge.

        isAttacking = false;
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.cyan;
        Gizmos.DrawWireSphere(this.transform.position, sightRange);

        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(this.transform.position, attackRange);
    }
}

Here is the Player Health

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerHealth : MonoBehaviour
{
    [Range(0, 200)] public int startHealth = 100, currentHealth;

    public static bool isDead;

    private void Start()
    {
        currentHealth = startHealth; // Set the current health to be the start health, when the game starts.
    }

    private void Update()
    {
        if (currentHealth <= 0 && !isDead)
        {
            isDead = true;
            Debug.Log("The Player has died!");
        }
    }

    public void TakeDamge(int amount)
    {
        currentHealth -= amount;
    }
}

Okay, I’ve skimmed over the code and I don’t see anything for the game “ending”. Are you asking how to do it?

Yes that is what I am curious about as I don’t know what to do

I really really love this approach: one step at a time.

Imphenzia: How Did I Learn To Make Games:

Thanks but its not what I’m really looking for as I need something simple to use as anytime I search the video is old or not in 3d and requires an entire new script

You are going to need many many scripts in your gamedev journey. In the video Kurt shared, it has an “end-of-level” state, and how to build that sequence of events. This is most likely very similar to the logic you are trying to do. Note, the logic does not care if the game is 3D or 2D.

-Player’s health reaches 0
-Stop game time
-Open UI panel that shows scores
-Have button for restart, next level quit, etc.

That is simplifying it a lot, but the video he shared is very good for beginners like yourself. There are also plenty of videos on End of Level stuffs that get straight to the point of what your are looking for. However, there is a lot of value in the video Kurt shared about the entire process:
9803637--1407480--upload_2024-4-29_12-40-26.jpg

1 Like

You have to be more specific, you can call Application.Quit() to end the whole game, but that would be a bit drastic. Do you want to return to a main menu, restart the current level, restart at a checkpoint or something else? No matter which of these choices you want to add, you will find a tutorial on youtube about these topics.

2 Likes

No, it actually IS what you’re looking for.

YOU need to learn how to do it and then YOU will learn exactly what you need and you will create it.

There’s not some huge nebulous cloud of “random useful scripts” orbiting out in the vast interrouter reaches of the internet just waiting to alight upon your computer and make magic happen for you. That’s not a thing.

YOU will be the one to do it, and that step-at-a-time approach is going to be the fastest and most efficient way to do it.


Send me a script / the scripts please!

https://discussions.unity.com/t/860348/2