Spawn enemy on nearest platform

Hi guys, i got a problem and i hope you guys will help me with a solution. i creating a game like doodle jump, so i am spawning a enemy, the enemy spawn just in a random place in air. i want i to spawn on a platform.

i want finalpostion to be equal to nearest platform position, every time the enemy spawns. heres the code:

using UnityEngine;
using System.Collections;

public class EnemySpawner : MonoBehaviour {

public GameObject[] Enemy;

public float MinTime = 2f;
public float MaxTime = 20f;

public bool spawning = false;

public SpawnerScript1 pos;

void Update()

	if (!spawning) 

IEnumerator SpawnEnemy()
	spawning = true;
	//Wait for random time before respawning
	yield return new WaitForSeconds(Random.Range(MinTime, MaxTime));
	//Set random position inside X&Y axis and materialize
	Vector2 finalposition = new Vector2(pos.position.x, pos.position.y - 1.2f);
	Instantiate(Enemy[Random.Range(0, Enemy.Length)], finalposition, Quaternion.identity);
	spawning = false;



Assume you have an array or list of platforms. If so, then compute the distance from your finalposition variable to each of the platforms, and choose the closest one. Then set finalposition to that platform position.

Keep a generic list for the platforms and add them in as they’re created. Then when spawning an enemy, loop through the list and compare their distances to your reference point. The platform with the smallest return is now your closest platform.