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?
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 !
“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”
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!
Not quite. First, create the MyCubeAppears script. Attach it to your same GameObject. So you should have both scripts attached to the same object:
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:
And drag the CubeAppears component (from the inspector) into the slot that appears: