Checking to see if BoxCollider intersects with another collider doesn't work.

Hi, my problem is that the bounds.Intersects doesn’t work when I check it (or is just not triggering a move).

What I’m trying to do is, I have generated many “cells” and those cells have BoxColliders attached to them, but when they get instantiated the size of the collider is determined.

Here is my Cell script (generating a quad just for visual representation so I can see whether or not it overlaps):

using UnityEngine;
using System.Collections;

public class CellSize : MonoBehaviour {

	public int x, z;

	void Start() {
		BoxCollider collider = this.gameObject.GetComponent<BoxCollider>();
		collider.size = new Vector3(x, 1, z);
		GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
		quad.transform.parent = this.transform;
		quad.transform.position = this.transform.position;
		quad.transform.eulerAngles = new Vector3(90, 0, 0);
		quad.transform.localScale = new Vector3(x, z, 0);
	}

And this is the part of code that I have in my GenerationManager script that should move the cells away from each other if they are intersecting:

private void Separate(Transform childObj) {

		BoxCollider childObjCollider = childObj.GetComponent<BoxCollider>();

		foreach (Transform child in this.transform) {
			BoxCollider childCollider = child.GetComponent<BoxCollider>();
			if (childObj.transform.name != child.transform.name) {
				while (childObjCollider.bounds.Intersects(childCollider.bounds)) {
					childObj.position += new Vector3(5, 0, 5);
					//childObj.position = child.transform.position + ((childObj.transform.position - child.transform.position).normalized * 5f);
				}
			}
		}
	}

But apparently it still doesn’t move the objects away from each other, but I put a debug.log inside the while loop, so I’m assuming the colliders know they are intersecting, but the objects just aren’t moving.

Here’s a picture showing the end result of this:

As you can see the colliders are still overlapped.

@TrivialRoo
It looks like you can achieve your result (and streamline your code) by checking “Is Trigger” in the Inspector Tab for your colliders and then using one or more of Unity’s built-in Trigger methods in your script:

OnTriggerEnter();
OnTriggerStay();  //This is the one you're apparently looking for
OnTriggerExit();

The Unity Team published a nice video on Trigger collision methods:
http://unity3d.com/learn/tutorials/modules/beginner/physics/colliders-as-triggers?playlist=17120

Once you detect the collision, you can add a repelling vector to the colliding object (called “other” in the code below). The second “AddForce” line applies the opposite force vector to the object running the script. (NOTE: this code passed muster in my compiler, but it’s after 5am and I need to go to bed so didn’t test it.)

void OnTriggerStay(Collision other)
{
  Vector3 repellingVector = new Vector3();
  repellingVector = other.transform.position - gameObject.GetComponent<Transform>().position;
            
  other.rigidbody.AddForce(repellingVector, ForceMode.Force);
  gameObject.GetComponent<Rigidbody>().AddForce(-repellingVector, ForceMode.Force);
}

“ForeceMode.Force” is the default way to apply physics forces and is included here only for illustration. Three other force modes are available in Unity 5.2 to modify the behavior of the physics engine in certain ways.