- Home /
How to destroy multiple box colliders through C# script
Hello, I have an enemy with multiple box colliders and an enemy script. What I need to happen is when the enemy dies, all the box colliders get destroyed (I am using Unity 2020.1.1f1 and Visual Studio 2019). here is my script:
 using UnityEngine;
 
 public class Enemy : MonoBehaviour{
 
 
     public Animator death;
 
     public float health = 50f;
 
     public void TakeDamage(float amount)
 
 
     {
         health -= amount;
         if (health <= 0f)
         {
             Die();
         }
     }
 
     void Die()
     {
         death.SetTrigger("IsDead");
         
     }
If anyone could help it would be GREATLY appreciated.
Answer by Baesickkk · Aug 14, 2020 at 05:05 AM
Use Destroy() to remove the collider from your enemy gameobjects. Here's the link buddy: https://docs.unity3d.com/ScriptReference/Object.Destroy.html
Here's the sample code for destroying components:
 void DestroyComponent()
     {
         // Removes the rigidbody from the game object
         Destroy(GetComponent<Rigidbody>());
     }
 
@Baesickkk this kind of works, but I have multiple colliders. And this only destroys one of them when the Die() function is called.
@TwiningLion1357 store your enemy gameobjects in an array/list then do a loop that will remove the colliders "for each" enemy.
@Baesickkk So sorry for the really late reply. I looping some stuff, but whenever I tried it, Unity crashed.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                