How to avoid gameObjects with different tags being spawned in eachother?

Hi.

I’m currently developing this game:
http://gdcgames.unf.dk/game.php?gid=76

In the game i have a spawner which keeps spawning the level infinitely.
I also have a script determining how many obstacles should be on each of those spawns, and i have fixed it so that obstacles with the same prefab, can’t spawn in eachother. However in the game there are currently 3 different obstacles, and i’m having troubles fixing it so obstacle 2 can’t spawn on top of obstacle 1 for example.

If you play the game, you will see that the spikes can spawn on top of the buzzsaws, and the eastereggs can spawn on top of the buzzsaws and spikes and so on. However, the spikes can’t spawn on top of spikes, buzzsaws on buzzsaws and eastereggs on eastereggs.

This is the code i use for the different obstacles in my levelScript:

	void Start () 
	{
		gameControlScript = GameObject.FindWithTag("GameController").GetComponent<GameControl>();
		levelAcceleration = gameControlScript.levelAcceleration;
		initialSpeed = speed;
		SpawnSpikes();
		SpawnBuzzsaws ();
		SpawnToilets ();
		SpawnKindereggs ();
	}
	
	void SpawnSpikes()
	{
		for(int i = 0 ; i < spikeAmount ; i++)
		{
			GameObject spikeInstance;
			Vector3 spawnPos = new Vector3(Random.Range (4.368514f,-4.305408f), Random.Range (0.61f,0.62f),Random.Range(-4.529316f,4.340265f));
			while (Vector3.Distance ( spawnPos, lastSpawnPos) < 4f)
			{
				spawnPos = new Vector3(Random.Range (4.368514f,-4.305408f), Random.Range (0.61f,0.62f),Random.Range(-4.529316f,4.340265f));
			}
			spikeInstance = Instantiate (spikePrefab,Vector3.zero,Quaternion.Euler(new Vector3(90,-180,0))) as GameObject;
			spikeInstance.transform.parent = transform;
			spikeInstance.transform.localPosition = spawnPos;
			lastSpawnPos = spawnPos;
		}	
	} 
	
	void SpawnBuzzsaws()
	{
		for(int i = 0 ; i < buzzsawAmount; i++)
		{
			GameObject buzzsawInstance;
			Vector3 spawnPos = new Vector3(Random.Range (4.368514f,-4.305408f), Random.Range (0.03678149f,0.03678149f),Random.Range(-4.529316f,4.340265f));
			while (Vector3.Distance ( spawnPos, lastSpawnPos) < 4f)
			{
				lastSpawnPos = new Vector3(Random.Range (4.368514f,-4.305408f), Random.Range (0.17459f,0.174591f),Random.Range(-4.529316f,4.340265f));
			}

			buzzsawInstance = Instantiate (buzzsawPrefab,Vector3.zero,Quaternion.Euler(new Vector3(90,-180,0))) as GameObject;
			buzzsawInstance.transform.parent = transform;
			buzzsawInstance.transform.localPosition = new Vector3(Random.Range (4.368514f,-4.305408f), Random.Range (0.03678149f,0.03678149f),Random.Range(-4.529316f,4.340265f));	
			lastSpawnPos = spawnPos;
		}	
	}
				

			void SpawnKindereggs ()
			{
				for (int i = 0 ; i < kinderAmount ; i++)
				{
					GameObject kinderInstance;
					Vector3 spawnPos = new Vector3(Random.Range (4.368514f,-4.305408f), Random.Range (0.03678149f,0.03678149f),Random.Range(-4.529316f,4.340265f));
					while (Vector3.Distance ( spawnPos, lastSpawnPos) < 4f)
					{
						lastSpawnPos = new Vector3(Random.Range (4.368514f,-4.305408f), Random.Range (0.17459f,0.174591f),Random.Range(-4.529316f,4.340265f));
					}
			
					kinderInstance = Instantiate (kinderPrefab,Vector3.zero,Quaternion.identity) as GameObject;
					kinderInstance.transform.parent = transform;
					kinderInstance.transform.localPosition = new Vector3(Random.Range (4.368514f,-4.305408f), Random.Range (0.17459f,0.174591f),Random.Range(-4.529316f,4.340265f));
				}
			}

And this is the script for the background spawner:

using UnityEngine;
using System.Collections;

public class BackgroundSpawner : MonoBehaviour 
{
	public Transform backgroundPrefab;
	
	
	public float zOffset = 0.01f;
	public bool applyOffset;
	
	public void Spawn(float displacement)
	{
		print (displacement);
		
		Vector3 position = transform.position;

		if(applyOffset)
		{
			position += Vector3.forward * zOffset;
		}
		
		position += Vector3.up * displacement;
		
		Transform newElement = (Transform)Instantiate(backgroundPrefab, position, backgroundPrefab.rotation);
		
		applyOffset = !applyOffset;
	}
}

The code is slightly long so I looked at it briefly.

But from your question, I can see you are doing something wrong.

You somehow check if an object of the same type has spwand in the same location.

What you should do, is check if any object has spwand in the same location.

after a brief look at the code, it seems you are checking it by saving a previous position (correct me if I’m wrong).

You might wanna think about a different way of doing it.

Options I can think of: collision and raycast.