Enemy spawning system in C#

I’m trying to create an enemy spawning system in C#. I’ve been following a tutorial and from that I got the basic code down but I’ve run into few issues. The main thing is that when an enemy spawns it spawns at the world origin, not where I have the object placed. Also, I was trying to get enemies to spawn at a random point within a sphere (that’s what I assumed the Random.insideUnitSphere did), I don’t know if it’s because every spawn default to the world origin or not (I’m assuming it is) but it’s the sphere/object itself that moves. I was hoping instead to have a static invisible object and an enemy to spawn within a sphere around the object.

public class RandomSpawner : MonoBehaviour {

    public GameObject[] spawnObject;
    public float radius = 1.0f;
	public float minSpawnTime = 1.0f;
	public float maxSpawnTime = 10.0f;
	public bool constantSpawn = false;
	
	
		void OnTriggerEnter(Collider ShipMaster)
		{
			Invoke("SpawnEnemy", Random.Range(minSpawnTime,maxSpawnTime));
		}
	
	
	
	
	void OnTriggerExit(Collider ShipMaster)
	{
		
		if (constantSpawn == false)
		{
			CancelInvoke("SpawnEnemy");
		}
	}
	

	void SpawnEnemy () 
	{
		float spawnRadius = radius;
		int spawnObjectIndex = Random.Range(0,spawnObject.Length);
			
		transform.position = Random.insideUnitSphere * spawnRadius;
			
		Instantiate(spawnObject[spawnObjectIndex]);
		
		if (constantSpawn == true)
		{
			Invoke ("SpawnEnemy", Random.Range (minSpawnTime,maxSpawnTime));
		}
	}
}

EDIT: Removed a bit of pointless code.

  • Create a new gameobject and place it somewhere where you want that object to spawn (sufficient height from ground)

  • I believe Instantiate takes 3 arguments, not one

    public Transform spawnPosition; //drag and drop created gameobject (spawn position) on this transform in inspector

      Instantiate(spawnObject[spawnObjectIndex],spawnPosition.position,spawnObject[spawnObjectIndex].transform.rotation);