How can i randomly create an array of objects without colliding one another?

Hello again! I am trying to create a script that could come up with a number of randomly generated positions to place objects. How can i avoid having the objects collide with each other? I know how to DETECT a collision (using Physics.OverlapSphere or CheckSphere) but how would i apply this to this script?

This is what I know:

var count:int = 5;
var prefab:Transform;

    ///This will create an array of objects in random positions.
    for(var i = 0; i < count; i++)
            var randPos = Vector3(Random.Range(-10, 10), Random.Range(-10, 10), Random.Range(-10, 10));
        newObj = Instantiate(prefab, transform.position, transform.rotation);

And just checking and repositioning the object isnt ideal either because it may never find a spot in the for-loop, which could crash unity. Anyone have any other approaches?

If you want use Physics.OverlapSphere and you objects have unique tag, for example, “mob”, that here small example(write on CSharp):

 public Transform myPrefab = null; //reference for your object
 public float myRadius = 2.0f; //Radius of the sphere described round object
 public int myCount = 5; //count created objects

 void Awake() {
  Vector3 randPos =;
  int myCheck = 0; //count overlap colliders
  for(int i = 0; i < myCount; i++) {
   do {
    myCheck = 0;
    randPos = Vector3(Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f));
    Collider[] hitColliders = Physics.OverlapSphere(ramdPos, myRadius);
    for(int j = 0; j < hitColliders.Length; j++) {
     if (hitColliders[j].tag == "mob") {
   } while (myCheck > 0);
   Transform newObj = (Transform) Instantiate(prefab, randPos, Quaternion.identity);  

But at the wrong initial parameters at you the infinite cycle can study. Be attentive. I hope that it will help you.