Spawning 2 objects in exactly the same place!!!

Hi All,
I have been using unity for about 2 weeks now and I’m fairly new to programming as well so please excuse my ignorance. I am trying to build a simple spawn mechanic that will spawn objects and send them towards the player. It appears to be working but I have noticed that sometimes there will be 2 objects in exactly the same place. This wouldn’t normally be a problem but it is causing my die() method to be triggered twice which causes life counting problems, here is my spawn script any help would be much appreciated. If you need any more information I will do my best to provide it.

#pragma strict
var block1 : GameObject; 
var scriptJunky: Transform;
var rand: int;
var xSpawn: int;
var xSpawnOld: int;
var k :int;
var xSpawns = new Array();
var xSpawnsFloat: float[];
var x: int = 0;
var clone : GameObject;
var startblock: GameObject;

function Start () {
xSpawnsFloat = [-2,-1.5,-1,-0.5,0,0.5,1,1.5,2];
InvokeRepeating("SpawnBlockTimer", 0, 0.6);
xSpawnOld = 1;
Random.seed = System.DateTime.Now.Second;


}

function Update(){
		
			xSpawn = Random.Range(0,9);
}


function SpawnBlockTimer() {

	var restartScript: RestartGUI = scriptJunky.GetComponent("RestartGUI") as RestartGUI;
	
	if(restartScript.isPaused == false){
	
			if(xSpawn < xSpawnOld){
				xSpawnOld = xSpawn + 3;
				SpawnBlock();
				Debug.Log("SpawnBlock < Triggered" + System.DateTime.Now);
			}else if (xSpawn > xSpawnOld){
				xSpawnOld = xSpawn - 3;
				SpawnBlock();
				Debug.Log("SpawnBlock > Triggered" + System.DateTime.Now);
			}else if (xSpawn == xSpawnOld){
				xSpawn = Random.Range(0,9);
		
			}
	
		}
			
			
	}


function SpawnBlock(){
				
				
				clone = Instantiate(block1, Vector3(xSpawnsFloat[xSpawnOld],0.45,20), block1.transform.rotation);
				//Render it visable
				var mesh : MeshRenderer = clone.GetComponent(MeshRenderer);
				mesh.enabled = true;
				
				//Destroy after x seconds
				Destroy (clone, 3);
				
				if(clone.activeSelf == true){
				clone.transform.tag = "Enemy";			
				}
				
				var enemies = GameObject.FindGameObjectsWithTag("Enemy");	
				
				//Check how many obsticles are alive	
					if(enemies.Length > 8){
					Destroy(clone);
					}
				yield WaitForSeconds(0.5);		
}

Thank you in advance!!!

Well, there are multiple solutions:

  • Add a collider (& rigidbody) to your objects to stop them from intersecting eachother. This might give weird results if 2 are spawned in the same place.
  • Remember where you spawned your last object, and if the new position is the same, get a new position.
  • The one I’d recommend: Fix your “die” method, so after an object hits your player, you set 1-2 seconds for the player to be invulnerable, so even if a second enemy hits him, life counters don’t go down. You can just use a bool and the Invoke method for this (have a bool like “isRespawning”, set it to true on the first hit and call an Invoke method that sets it to false after 2 sec. When you are hit, check that “isSpawning” is false, if it is true, do nothing). I don’t know what game you are building, but lots of games have this mechanic.