Home > other >  How can I check multiple collider Triggers at once?
How can I check multiple collider Triggers at once?

Time:07-12

I have an aircraft in my game that is controlled by the player. I want it to get destroyed after it collides with something. The aircraft has many colliders on it, so im using an array. Heres my code:

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

public class AircraftDamage : MonoBehaviour
{
public Collider[] Colliders;
public GameObject Aircraft;


 private void OnTriggerEnter(Colliders collision)
 {
    DestroyAircraft();
 }






 public void DestroyAircraft()
 {
    Destroy(Aircraft);
 }

}

Im getting the obvious error here, type or namespace 'Colliders' could not be found. How can I check if any collider within my array gets triggered?

CodePudding user response:

a better approach is to use OnCollisionEnter instead of OnTriggerEnter

CodePudding user response:

You are missing the basics of Collision here. You don't need to reference the Colliders using an array.

Both OnCollisionEnter and OnTriggerEnter are called when the gameobject to which the script is attached collides with another collider. They return the collider with which it has collided.

So in your case its better to have a single collider on the parent gameobject rather than separate Colliders.

If you really want to have separate colliders for the airplane's body part then you need to add a collision checking script to each of the gameobject or you can add the collision checking script to your obstacles like building and have all your Airplane parts in a single tag.

Check out this guide on Unity collision

  • Related