- Home /
OnTriggerStay2D with many colliders
OnTriggerStay2D returns only 1 random collider within trigger collider, how can i get ALL others colliders?
Comment
Answer by AngelFerrari · Nov 20, 2015 at 06:20 PM
private List<GameObject> Targets = new List<GameObject>();
// Here we initialize a List to fill with diferents GameObjects.
void Update () {
// Here we read the list.
for (int Cont = 0 ; Cont < Targets.Count; Cont++) {
Targets [Cont]. // ---> Here (Next to the dot) you get the component that you want.
}
}
/* With the OnTriggerEnter2D & OnTriggerExit2D you "add" the diferents GameObjets to the List<Targets> - Only if they have the Tag = "Enemys". For example, you can create diferent list for diferent GameObjects or no if you dont want to. In That case, just dont use the "if" for the tags. But be careful, diferents game objects in a list can have diferents components (Ej: Diferents Scripts). */
void OnTriggerEnter2D (Collider2D Col)
{
if (Col.tag == "Enemy")
{
Targets.Add (Col.gameObject);
}
}
void OnTriggerExit2D (Collider2D Col)
{
if (Col.tag == "Enemy")
{
Targets.Remove (Col.gameObject);
}
}
// That is how i do to get multiples elements on a Collider.
I know this is an old question , but I had to solve this problem on my own in the last days . Maybe you already resolved , but maybe someone else is looking for this solution . :D