- Home /
Getting an array of all triggers in the scene
Hi. Is there a way I can get an array of all the triggers in my scene?
Comment
Answer by JustFun · Aug 31, 2014 at 05:51 PM
Yes. This function allows to enumerate all colliders in scene, and then it's need to check IsTrgger value:
using System.Collections.Generic;
public class Triggers : MonoBehaviour
{
private List<GameObject> objectsWithTriggers = new List<GameObject>();
void Start ()
{
Collider[] cols = FindObjectsOfType<Collider>();
foreach(Collider col in cols)
{
if(col.isTrigger)
{
objectsWithTriggers.Add(col.gameObject);
}
}
}
FindObjectsOfType() is slow! Keep it in mind.
Your answer
![](https://koobas.hobune.stream/wayback/20220613161954im_/https://answers.unity.com/themes/thub/images/avi.jpg)