Enemy random generator

Hi

This script work fine but the “Enemy” generating from the middle of screen! (One by one) i need to make more than one with a random position and i want it to generate from other position not in middle.

Thanks…

using UnityEngine;
//using System;
public class Generate : MonoBehaviour
{
	public GameObject mines;
	
	// Use this for initialization
	void Start()
	{
		//Random random = new Random ();
		float x = Random.Range (2,5);
		InvokeRepeating("CreateObstacle", 3f, x);
	}
	
	void CreateObstacle()
	{
		Instantiate(mines);
	}
}

Well, you’re using the wrong overload of Instantiate in that case.
Instantiate(GameObject); will instantiate your wanted object at the middle of your world space(Vector3.zero), with the default rotation.

You want to use:
Instantiate(GameObject, Vector3, Quaternion); which also has location and rotation in its’ parameters.

Consider using:

Instantiate(mines, spawnPos, Quaternion.identity);

In the create obstacle function.
Of course, you’d still need to define spawnPos for the different positions you want your mines to appear at.
Lastly, Quaternion.identity just means that the item spawns at the default rotation you defined for it in its’ prefab, so just keep that one unless you want a different rotation.

Hope that helps.