Hello
I have little problem. I have gameobjects in parent gameobjects.
How to detect if ALL childs is trigger exit?
(I tried set bool to the every child when is trigger exit and than chceck in parent if all childs bool is true… It is nonsense of course)
Sorry for my english, is not my native language.
//This array will hold the children
public GameObject[] childrenObj;
void Start()
{
//This will get all the children of the object you want
int children = transform.childCount;
childrenObj = new GameObject[children];
for (int i = 0; i < children; i++)
childrenObj[i] = transform.GetChild[i];
}
void OnTriggerExit(Collider other)
{
//Now we check if each object is colliding with "Something" tag.
if(other.tag == "Something")
{
for(int i = 0; i < children; i++)
print(childrenObj[i].name);
}
}
ummm… I had something similar but, parent of gameobjects dont have collider, i want check if childs triger exit from third gameobject… I forgot to mention that the parent is only “group” for childs…
I know, the parent does not matter as long as it exists.
What you want to do is:
for(int i = 0; i < children; i++)
childrenObj[i].GetComponent<BoxCollider>().triggering = true;
When you take the code I gave you in the first place, and let it archive the children, you can do whatever you want with them and access them from anywhere at all!
As I said in my first code, I wrote:
Where it says “if(other.tag == “Something”)” is where we check what the other collider is called/tagged/anything you want to check. In there, you can write the name of the other ‘third’ object you’re talking about, then you can just say:
for(int i = 0; i < children; i++)
childrenObj[i].GetComponent<BoxCollider>().triggering = true;
where it says “print(childrenObj*.name);”*
Ummm okay, that is good. I had it similiar, but i want destroy parent, ONLY if all childs are trigger exit. And this is problem, because if any child is trigger exit, bool set to true. I dont know how to check if ALL bool are true.
Oh I see.
You’ll need a bool array in that case, like this:
public static bool[] triggering;
public static int amountNotTriggering; //We'll use this one to increase the amount of bools true
void Start()
{
triggering = new bool[children];
}
And now just paste this into your OnTriggerExit():
void OnTriggerExit(Collider other)
{
//Now we check if each object is colliding with "Something" tag.
if(other.tag == "Something")
{
for(int i = 0; i < children; i++)
{
triggering[i] = false;
}
}
}
After that, you can use Update() to check if all the bools are true!
void Update()
{
for(int i = 0; i < children; i++)
{
if(triggering[i] == false)
amountNotTriggering += 1;
}
}
And now finally, we can check to see if amountTriggering is equal to the amount of children, meaning all the bools are true:
//Paste this into void Update()
if(amountNotTriggering== children)
{
print("Amount of objects not triggering anymore: " + amountNotTriggering);
Destroy(this.gameObject); //This will destroy the current transform as you wanted
}
Basically,
we setup a new boolean with amount of elements equal to the amount of children. Then, we check for each child object if they’re triggering or not, and if they’re not, the bool specific for them gets set to false. And for each bool that is false, we increase the int amountNotTriggering by 1. At the end, we check if amountNotTriggering is equal to the amount of children there are, and if it is then we do something!
awesome, thanks
You’re welcome ![]()
If you ever get stuck, don’t hesitate to ask!