I want to enable my collider again after disabling it

536 Views Asked by At

When my character hits the gameobject collider, an enemy will be spawned and the collider is disabled, cuz I do not want to spawn multiple enemies. When my character dies and I have to start from the beginning, the collider should be enabled again to spawn the enemy again.

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

public class TriggerSpawner : MonoBehaviour
{
   public EnemySpawn enemyspawn;

    
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            enemyspawn.SpawnEnemy();
            gameObject.GetComponent<BoxCollider2D>().enabled = false;
        }
    }
    
}

//in other class
private void SetHealth(float health)
        {
            var actualNextHealth = Mathf.Min(m_maxHealth, health);
            m_currentHealth = actualNextHealth;
            if (m_healthBar != null && m_maxHealth > 0f)
                m_healthBar.SetHealth(actualNextHealth / m_maxHealth);

            if (m_currentHealth <= 0f)
            {
                UpdateHighscore();
                Die();
            }
        }

  private void Die()
        {
            m_character.NotifyDied();

            if (m_canRespawn)
            {
                SetVulnerable();
                RemovePoison();
                m_hazards.Clear();
                gameObject.transform.position = m_spawnPosition;
                SetHealth(m_maxHealth);
            }
            else {
                Destroy(gameObject);
            }
        }
2

There are 2 best solutions below

4
iFralex On

You can create a static variable in the trigger script that you assign the Collider value to it. When an enemy is spawned it deactivates, as in your code.

public class TriggerSpawner : MonoBehaviour
{
   public static Collider2D spawnCollider;
   public EnemySpawn enemyspawn;

    void Start() => spawnCollider.GetComponent<Collider2D>();

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            enemyspawn.SpawnEnemy();
            spawnCollider.enabled = false;
        }
    }
}

When you die, it will reactivate.

private void Die()
        {
            m_character.NotifyDied();

            if (m_canRespawn)
            {
                TriggerSpawner.spawnCollider.enabled = true;
                SetVulnerable();
                RemovePoison();
                m_hazards.Clear();
                gameObject.transform.position = m_spawnPosition;
                SetHealth(m_maxHealth);
            }
            else {
                Destroy(gameObject);
            }
        }
1
KYL3R On

With minimal changes on your code, I'd suggest this:

private void Die()
    {
        m_character.NotifyDied();

        if (m_canRespawn)
        {
            SetVulnerable();
            RemovePoison();
            m_hazards.Clear();
            gameObject.transform.position = m_spawnPosition;
            SetHealth(m_maxHealth);
            gameObject.GetComponent<BoxCollider2D>().enabled = true; // add this line
        }
        else {
            Destroy(gameObject);
        }
    }