- Home /
How do I get List of all objects touching during a collision
During a collision, is there a way to get a list of all objects that are touching the object just collided with?
Answer by sumeetkhobare · Feb 17, 2015 at 10:15 PM
Try looking into Physics.OverlapSphere It checks for colliders around your gameobject in a specific radius. There are other 'Overlap*' functions like circle, box, etc. But it won't take in consideration your gameobject's collider(which may be complex like enveloping your whole mesh).
$$anonymous$$ark the question as solved if it helps.
Answer by reinfeldx · Feb 18, 2015 at 03:26 AM
Try this. Let me know if you have any questions.
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // Don't forget to add this if using a List.
public class CollisionList : MonoBehaviour {
// Declare and initialize a new List of GameObjects called currentCollisions.
List <GameObject> currentCollisions = new List <GameObject> ();
void OnCollisionEnter (Collision col) {
// Add the GameObject collided with to the list.
currentCollisions.Add (col.gameObject);
// Print the entire list to the console.
foreach (GameObject gObject in currentCollisions) {
print (gObject.name);
}
}
void OnCollisionExit (Collision col) {
// Remove the GameObject collided with from the list.
currentCollisions.Remove (col.gameObject);
// Print the entire list to the console.
foreach (GameObject gObject in currentCollisions) {
print (gObject.name);
}
}
}
So basically what I said? lol
"Give the man a fish and he will eat for a day, but $$anonymous$$ch him to fish and he will eat for a lifetime."
I've found that reviewing others' commented code is the best way to $$anonymous$$ch myself how to fish ;)
Anyway, I'm new round these parts and this was my first stab at an answer. Faux pas? $$anonymous$$ods, feel free to merge if necessary.
This is useful, but I've heard that OnCollisionExit is not always reliable. That's why I didn't accept this answer.
hi, im currentli using the code you pist, but how can i update it?, im making a rubiks cube, and it only works for me fot the fisrt move, i nead to update the list so i can move the diferent objects.
Answer by bartm4n · Feb 18, 2015 at 01:56 AM
I don't believe that there is a built in way to do this, but alas it is still possible. I'm not going to write the code for you, but I will give you some tips.
I'm not 100% sure how you are planning to use this information so that might slightly change how you would implement this.
With that said, you should look into OnCollisionEnter and OnCollisionExit. You would also maintain a list in the same script to track active collisions.
During OnCollisionEnter you would add the collided-with GameObject (derived from the Collision info) to the list and during OnCollisionExit you would remove the GameObject from the list.
I hope that this helps. Good luck!
What do you mean by:
"You would also maintain a list in the same script to track active collisions."
How do you do this? How do you get a list of objects "currently" colliding with the collider? For instance, Enter/Exit doesn't always work because sometimes Exit is never called on (for instance, when the object collider is disabled or changes layers.
Your answer
![](https://koobas.hobune.stream/wayback/20220613183609im_/https://answers.unity.com/themes/thub/images/avi.jpg)