Spawn Script not working properly, objects overlapping

My problem is my Spawn script, I have one object attached to it. and when they spawn the object sometimes overlaps each other, or spawns above the spawn area I have it spawning in, not sure what to do. Here’s my code:

using UnityEngine;
using System.Collections;

public class SpawnScript : MonoBehaviour {

	public GameObject[] obj;
	public float spawnMin = 1f;
	public float spawnMax = 2f;
	
	
	void Start () 
	{
		Spawn();
		
	}
	
	void Spawn()
	{
		Instantiate(obj [Random.Range (0, obj.GetLength (0))], transform.position, Quaternion.identity);
		Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
	}
}

It also doesn’t matter what I put in my spawnMin, and spawnMax, they overlap. This is for my floor, if at all possible I would like them to spawn right next to each other, with no gap, but I seen to can’t figure that out either.

Thanks for the help.

Ofcourse they overlap. You are spawning them at the same position. If you want the to spawn next to each other, add a counter and do this:

var counter : int;

function Spawn()
{
      Instantiate(obj [Random.Range (0, obj.GetLength (0))], Vector3(transform.position.x + counter*5,transform.position.y,transform.position.z), Quaternion.identity);
      counter++;
}

Also, I would put a timer in Update and not use invoke inside the function itself.

Thanks, but it doesn’t work, I think you have it written in java, and I use C#, I have no idea how to change it.

int counter;
and the Vector is created using “new”

new Vector3(transform.position.x + counter*5,transform.position.y,transform.position.z)

Thanks, no errors, now my spawners are only spawning one thing. I’m guessing it’s the counter++; at the end, hows that suppose to be coded?

Also you said something about a timer in update?

Ok, I got a counter running now, but the floor objects are still overlapping each other, no matter what time I put in the counter.

Here’s my code

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class SpawnScript : MonoBehaviour {

	public GameObject[] obj;
	//public float spawnMin = 1f;
	//public float spawnMax = 2f;

	public float counter = 1f;

	void Start () 
	{
		Spawn();
		
	}


	void Spawn()
	{
		Instantiate(obj [Random.Range (0, obj.GetLength (0))], new Vector3(transform.position.x + counter*5, transform.position.y, transform.position.z), Quaternion.identity);
		//Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
		counter++;
	}

	void Update()
	{
				counter -= Time.deltaTime;

		if (counter <= 0)
						
		{
						
				counter = 0;
				Spawn ();
		}
		else
	{

			counter = counter;



	}
}
}

Is there a way to tell the spawn not to spawn any objects till the object before it is out of the way?

Ok, then above script kind of works. my problem now is if my gameobjects are touching, and when they hit my destroyer, all the objects that are touching are destroyed, even the ones on screen. How do I make the gameobjects spawn but without touching? I mostly have been using the first spawn script posted, the second doesn’t work as good.