Multiple colliders inside a trigger, activate something

Hi guys !

I’m looking for a solution who can enable be to activate something when I have three gameobject inside a trigger ?

Let’s say that as long you haven’t three gameobject inside a trigger, nothing happened.

But, as soon a third gameobject come inside the trigger, something happened.

Thanks for helping !

I’m new at unity ! :slight_smile:

Make a variable that is a List of your objects.

When you see OnTriggerEnter for an object, add it to the list.
When you see OnTriggerExit for an object, remove it from the list.

When you se the length of the list become 3 or greater, you can activate whatever you want to activate.

Wooow really fast !! :smile: Thanks !

I started this to create a list :

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

public class TriggerCOunt : MonoBehaviour
{

public GameObject player;

// Start is called before the first frame update
void Start()
{
StartCoroutine(myCoroutine())

}

IEnumerator myCoroutine()
player.Add(objectToAdd);

yield return new WaitForSeconds(3f)

It is correct ?

That’s not what I described. I described a list containing objects. You have written a coroutine that waits for 3 seconds for some reason?

Did you want to do something when your object has been inside the trigger for 3 seconds, or did you want to do something when 3 objects are inside the trigger?

Noooo XD

I’m… really… bad at coding so I wrote “How create a list” on the net and follow what the guys says XD

Don’t blame me pleaassse…

I understood what you said … And you are right…

The truth is that I have absolutly no ideas of how create a list, how add an object as soon is enter on trigger and how activate something when the list become three or greater…

On my mind it should be like this :

List : “number of gameobject inside the trigger”;

When a gameobject enter then list++;

Maximum of gameobject inside the trigger = “3”

if “number of gameobject inside the trigger” = “Maximum of gameobject inside the trigger”

then “Something happened”

I really want to do something when 3 objects are inside the trigger ! Not about times but about numbers !

I’m sooo sorry X) Thanks for helping

I would do it like this:

public class TriggerCount : MonoBehaviour {
  public int triggerAmount = 3;
  public UnityEvent OnAmountReached;

  HashSet<Collider> collidersInside = new HashSet<Collider>();

  void OnTriggerEnter(Collider other) {
    collidersInside.Add(other);

    if (collidersInside.Count == triggerAmount) {
      OnAmountReached.Invoke();
    }
  }

  void OnTriggerExit(Collider other) {
    collidersInside.Remove(other);
  }
}

Then in the inspector you can use the UnityEvent to trigger whatever function you want on any object in the scene.

So here :

“public int triggerAmount = 3;” is : You set the max numb at 3

“public UnityEvent OnAmountReached;” is : When you reach the numb 3, do something.

“HashSet collidersInside = new HashSet();” is : When something is inside the collider, add 1 to the list.

“OnTriggerEnter(Collider other) {
collidersInside.Add(other);” is : Continue looping until you arrive to 3

“if (collidersInside.Count == triggerAmount) {
OnAmountReached.Invoke();” is : When you reach 3, do something

“void OnTriggerExit(Collider other)
collidersInside.Remove(other);” is : If one of the gameobject leave and the number become less than 3, stop the function.

I’m correct ? (I want to understand)

A question ! :

Lets say that the “do something else” is “When the numb is 3, make a cube appears”

Which is :

mycube.SetActive(True);

Where did I put it on the script ?

Oh sorry ! Perhaps I create an other script “mycube.SetActive(True);” and put it on the UnityEvent ?

1 Like

Yep you pretty much got it all. You can either put it on another script and trigger it from the UnityEvent, or just add the code here at the place where I am invoking the event.

I would favor putting it on another script to keep this one focused!

Ok so lets see I create a first script called “MyCubeAppears”
Then this one.

I just have to replace “UnityEvent” by “MyCubeAppears” Which should be like this :

public class TriggerCount : MonoBehaviour {
public int triggerAmount = 3;
public MyCubeAppears OnAmountReached;

And then Unity will take the script “MyCubeAppears” and execute it ?

Not quite. First, create the MyCubeAppears script. Attach it to your same GameObject. So you should have both scripts attached to the same object:

6777224--783923--upload_2021-1-28_17-26-35.png

Then you Add a function to your CubeAppears script to make the cube appear:

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

public class CubeAppears : MonoBehaviour
{
    public void MakeCubeAppear() {
        Debug.Log("Cube appears!");
    }
}

Then go back to the inspector, and press this button:

6777224--783920--upload_2021-1-28_17-26-20.png

And drag the CubeAppears component (from the inspector) into the slot that appears:

6777224--783929--upload_2021-1-28_17-27-44.png

Finally, find your function in the dropdown list:
6777224--783932--upload_2021-1-28_17-28-28.png

At the end it should look like this:
6777224--783935--upload_2021-1-28_17-29-19.png

:open_mouth:

Just thanks for all your patience and explanations !!!

God bless you !