random spawn position

Hello i have problem with random position spawning items
how i can make costume spawn point like
x = Random.Range(-300,300)
y = Random.Range(-150,360)
someone have any idea ? and i making 2D game.
i have code like this but it spawn only 1 location

using UnityEngine;
using System.Collections;

public class slapspawn : MonoBehaviour{
	public GameObject enemy;                // The enemy prefab to be spawned.
	public float spawnTime = 3f;            // How long between each spawn.
	public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
	
	
	void Start ()
	{
		// Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
		InvokeRepeating ("Spawn", spawnTime, spawnTime);
	}
	
	
	void Spawn ()
	{	
		// Find a random index between zero and one less than the number of spawn points.
		int spawnPointIndex = Random.Range (0, spawnPoints.Length);
		
		// Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
		Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
	}
}

Your script is using an array of transforms to randomly select one where the enemy will spawn. If you want to instead use a random range, just simply get a random value for x and y and then make it a Vector which you can use to determine the spawn position in Instantiate function. Code will be something like

     void Spawn () {    
    int spawnPointX = Random.Range(-300, 300);
	int spawnPointY = Random.Range(-150, 360);
	Vector3 spawnPosition = new Vector3(spawnPointX, spawnPointY, 0);

    Instantiate (enemy, spawnPosition, Quaternion.Identity);
   }

Where you select your spawn points with Random.Range you left out the range, use

  • int spawnPointIndex = spawnPoints [Random.Range(0, spawnPoints.Length)]