you can track and keep the number or count of the objects present in your collider. You can declare a variable count and in your OnTriggerEnter() increment it by 1 and in your OnTriggerExit() decrement it by 1. So now the value of count will tell you the number of objects present inside the collider.
First, non-collider trigger solutions are easier but less accurate for this task. For example, Physics.OverlapSphere(). But there is a way to do it with a trigger.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Bug2 : MonoBehaviour {
private List<Collider> colliders = new List<Collider>();
void Update () {
if (colliders.Count == 1) {
Debug.Log ("Do action based on 1");
}
else if (colliders.Count == 2) {
Debug.Log ("Find the nearest and do action for 2");
}
}
void OnTriggerStay(Collider col) {
colliders.Add (col);
}
void LateUpdate () {
colliders.Clear ();
}
}
At each frame, when you reach Update() ‘colliders’ will be a list of all the colliders found in the OnTriggerStay(). You can compare the two (or more) to see which is closest.
I will explain you more what the guys were trying to tell you,
I will use tags.
assign a tag to your objects that will enter the trigger.
Here i have assigned “guest”.
using UnityEngine;
using System.Collections;
public class MyClass : MonoBehaviour
{
private int count = 0;
void Update ()
{
if (colliders.Count == 1)
Debug.Log ("Do action based on 1");
else if (colliders.Count == 2)
Debug.Log ("Find the nearest and do action for 2");
}
void OnTriggerEnter(Collider col)
{
if(col.transform.tag == "guest")
count++;
}
void OnTriggerExit(Collider col)
{
if(col.transform.tag == "guest")
count--;
}
}