简体   繁体   中英

I want to enable my collider again after disabling it

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM