Why isnt my player being deleted and the scene changed?

I have these 2 scripts here that together should control the enemies ability to kill the player and also send them back to the main menu scene i have set up. But its not working and even chatgpt cant tell me why. using UnityEngine; using UnityEngine.AI; using UnityEngine.SceneManagement; public class RedEnemyAi : MonoBehaviour { public int health = 30; private int damage = 10; // Assuming a constant damage value for simplicity float attackDistance = 2; public Transform player; // Reference to the player NavMeshAgent agent; bool isAttacking = false; void Start() { agent = GetComponent<NavMeshAgent>(); player = GameObject.FindGameObjectWithTag("Player").transform; // Find the player object and set it as the target } void Update() { if (!isAttacking) { if (player == null) { // Find the player object if it's null player = GameObject.FindGameObjectWithTag("Player").transform; } else { float distanceToPlayer = Vector3.Distance(transform.position, player.position); if (distanceToPlayer <= attackDistance) { agent.isStopped = true; // Stop moving isAttacking = true; InvokeRepeating("Attack", 0f, 1f); // Call Attack method repeatedly //Debug.Log("Attack!"); } else { agent.isStopped = false; // Resume moving agent.SetDestination(player.position); // Set player's position as the destination } } } } void Attack() { if (player != null && Vector3.Distance(transform.position, player.position) <= attackDistance)
7 Replies
mr.killmeplease
mr.killmeplease3mo ago
{ // Reduce player's health Debug.Log("Attack successful!"); PlayerHealth playerHealth = player.GetComponent<PlayerHealth>(); if (playerHealth != null) { playerHealth.currentHealth -= damage; // Reduce player's health if (playerHealth.currentHealth <= 0) { Destroy(gameObject); SceneManager.LoadScene("Main Menu"); playerHealth.maxHealth = 100; } } } } } and then the player health script: using UnityEngine; using UnityEngine.SceneManagement; public class PlayerHealth : MonoBehaviour { public int maxHealth = 100; public int currentHealth; void Start() { currentHealth = maxHealth; } }
Anchy
Anchy3mo ago
$codegif
Anchy
Anchy3mo ago
Have you tried setting a breakpoint? does the players health actually reach 0 or below does your script actually even execute that far you can test these things with a breakpoint as a side note: you may want to create some properties or public methods in your PlayerHealth class that modify the player health instead of exposing the health fields directly
mr.killmeplease
mr.killmeplease3mo ago
im not sure what a breakpoint is but i figured out that its something with the reduction of health because the attack successful debug worked. But for some reason instead of killing the player after getting hit 10 times, it just continued running. also i tried that codegif but it says theres too many characters for discord to handle
Anchy
Anchy3mo ago
I would heavily recommend looking into breakpoints for debugging
mr.killmeplease
mr.killmeplease3mo ago
ok thx