- Home /
multiple Objects Enter OnTriggerEnter
I have a bomb had a TriggerCollider and i have 3 same Soldiers objects(same object Prefab) and i want when them Enter the bomb Collider :its destroy all three Soldiers at once together.
the problem its destroy only one object.
That's strange. OnTriggerEnter should be called for every kinematic and Rigidbody that intersects the trigger. Are you destroying the prefabs directly from the parameter like this?
void OnTriggerEnter (Collider other)
{
Destroy(other.gameObject);
}
yes destroying the prefabs directly from the parameter
The only reason I can think of OnTriggerEnter not working is if the rigidbody is sleeping. You could use WakeUp() in the soldier's script, or use the bomb script to find all the soldiers with FindWithTag() and destroy the one's that are within a set distance using a for loop.
This seems to be saying that when one of the 3 enters the trigger, you want all three to be destroyed. Is that right? The problem is that it only destroys the one that enters the trigger?destroy all three Soldiers at once together
Answer by veermalik · Jun 01, 2020 at 05:05 AM
@thecaesar because ontriggerenter detect a single collision at one time. you need to store the gameobjects of the players which enters in collider as a list of objects and then you can destroy all of them at once.
Answer by Kudorado · Jun 01, 2020 at 08:12 AM
I think you might try Physics.OverlapSphere instead, it will get all trigger colliders at once then return an array objects for you, just loop though all and destroy them.
https://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html
Your answer
Follow this Question
Related Questions
Colliders that do not collide... 1 Answer
Collision problem in C# 4 Answers
Trigger doesn't work 100% of the time. 1 Answer
Trigger OnTriggerEnter Not Triggering Properly on Collision 0 Answers
Keep doing something while colliding 1 Answer