Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

using System.Collections; using System.Collections.Generic; using UnityEngine; public…

  1. User

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

    public class BossHealth : MonoBehaviour { public int maxHealth = 5; private int currentHealth;

    private void Start()
    {
        currentHealth = maxHealth;
    }
    
    public void TakeDamage(int damageAmount)
    {
        currentHealth -= damageAmount;
    
        if (currentHealth <= 0)
        {
            Die();
        }
    }
    
    private void Die()
    {
        // Destroy the enemy object when health reaches 0
        Destroy(gameObject);
    }
    

    } make it so if the health reaches 0, the scene changes to Level2

  2. Assistant

    You can use the SceneManager class in Unity to change the scene when the boss's health reaches 0. Here's the modified code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class BossHealth : MonoBehaviour
    {
        public int maxHealth = 5;
        private int currentHealth;
    
        private void Start()
        {
            currentHealth = maxHealth;
        }
    
        public void TakeDamage(int damageAmount)
        {
            currentHealth -= damageAmount;
    
            if (currentHealth <= 0)
            {
                Die();
            }
        }
    
        private void Die()
        {
            // Destroy the boss object when health reaches 0
            Destroy(gameObject);
    
            // Check if the current scene is "Level1" and change to "Level2" if true
            if (SceneManager.GetActiveScene().name.Equals("Level1"))
            {
                SceneManager.LoadScene("Level2");
            }
        }
    }
    

    Make sure to add the using UnityEngine.SceneManagement; at the top of the script to access the SceneManager class. Replace "Level1" and "Level2" with the actual names of your scene files.