Get all currently colliding objects

Hi folks,

since I’m new to unity I decided to go easy and learn the ropes by creating a physics powered version of same game (SameGame - Wikipedia) with spheres, cubes, wedges and other objects.

I once coded an old school version of said game and used a 2d array/recursion to find adjacent blocks, but now the playing field is a mess and I guess I have to detect all collisions of an object and then run the same code recursively on them. I know of the OnCollision… functions and could probably keep track of all collisions that way, but having 180 rigidbodies with an n-sized array attached and constant collision checks sounds awfully slow and unnecessary.

So does anybody know how I can detect collisions on demand? I thought about Physics.OverlapSphere(…), but that doesn’t sound too precise with meshes like cubes and whatnot.

Thanks in advance!

I’d have a script for each object that holds an array of other objects that are touching it. Since you’d only add to the array in OnCollisionEnter and remove from it in OnCollisionExit, I can’t imagine that it would be slow.

–Eric

You’re right, that was much simpler than I thought. Thanks :slight_smile:
Guess I was once more daydreaming of nested for-loops… (in a nightmarish way)

Hi guys,

I’m importing a cube object from Blender.

This MasterCube has children which are faces of each face (like a rubix cube).

I’m trying to write an initialization script that detects which child objects are adjacent to each other.

I cannot do a simple gameObject.AddComponent(“RigidBody”);

or gameObject.AddComponent(RigidBody); for some reason!!?

Eric5H5, what type of script are you suggesting to obtain the array of objects that are “Touching” another object?

Ideally, all of the cube objects (parents/children) are static. No rigidbodies, logically.

Must I attach rigidbodies to each object to determine which objects are “touching”???

WHEN I try to script the following on a gameObject:

 gameObject.AddComponent("RigidBody");

I get the error: "Can’t add component because class ‘RigidBody’ doesn’t exist

WHY?

Thanks in advance…

have you tried “Rigidbody” ? notice the “b” minus