How do I spawn an object from multiple spawn points?

Hi everyone, I’m new to unity and haven’t had a lot of experience. Tried going through several tutorials and through forum posts but wasn’t able to figure out a way to make my script work.

I’m trying to spawn an Asteroid object from several different spawn points repeatedly. I’ve made several spawn points and placed them as children of an empty object where I’ve placed the script. If someone could take a look at my script and point out some of the many errors I’m sure I’ve made, I’d appreciate it.

Also, if you could suggest a way to destroy the objects after around 60 second, that’d be great. I’ve tried using Destroy (GameObject, time) with no success.

//Prefab Object
    var asteroidPrefab : Rigidbody;
    //Array of Spawn Points
    private var spawnPointList:GameObject[];
    //Variable to hold number of Spawn Points
    private var sPLength: int;
    //this is the time inbetween each instantiate, you can edit this time in the inspector to make the time between shots longer
    public var spawnInterval : int = 3.0;
    //this is how fast your cannonball will be fired, change this in the inspector depending on how big your level is
    public var asteroidSpeed : int = 100;
     


function Awake()
{
	//retrive list of spawn points
	spawnPointList = gameObject.FindGameObjectsWithTag("AsteroidSpawnPoint");
	
	//Size of Array
	sPLength = spawnPointList.Length;

}

function Update()
{
	AsteroidSpawn();
}

function AsteroidSpawn()
{

	//Random spawn point number
	var randomSpawnIndex = Random.Range(0,sPLength);
	    
	//Select random spawnpoint to spawn asteroid
	var asteroidSpawnPosition : Vector3 = spawnPointList[randomSpawnIndex].transform.position;
	    
	//Creates clone of the asteroid prefab
	clone = Instantiate(asteroidPrefab, transform.position, transform.rotation);
	    
	//Sets random velocity vector
	clone.velocity = Vector3(Random.Range(-100,-200), Random.Range(-100,100), Random.Range(-100,100));
	    
	//Spreads Asteroids
	randomScalar = Random.Range(100,1500);
	
	clone.transform.localScale=Vector3.one*randomScalar;
	
	Debug.Log ("Asteroid has just spawned");
	
	yield WaitForSeconds(spawnInterval);
		
    
}

I have made a few suggestions to relevant code below that should help with spawning the asteroids after the spawnInterval and destroying the object. If you are having any other specific problems please let us know exactly what is going wrong :slight_smile:

function Update() 
{	
	//Remove this since it spawns a new asteroid every frame regardless of spawnInterval.
    //AsteroidSpawn();
}

//Add this function to continuously spawn new asteroids
function Start() 
{
	InvokeRepeating("AsteroidSpawn", spawnInterval, spawnInterval);
}

function AsteroidSpawn() 
{
    //Random spawn point number
    var randomSpawnIndex = Random.Range(0,sPLength);
    //Select random spawnpoint to spawn asteroid
    var asteroidSpawnPosition : Vector3 = spawnPointList[randomSpawnIndex].transform.position;
    //Creates clone of the asteroid prefab
    var clone : GameObject = Instantiate(asteroidPrefab, transform.position, transform.rotation);
    //Sets random velocity vector
    clone.velocity = Vector3(Random.Range(-100,-200), Random.Range(-100,100), Random.Range(-100,100));
    //Spreads Asteroids
    randomScalar = Random.Range(100,1500);
    clone.transform.localScale=Vector3.one*randomScalar;
    
    //Add this to destroy the asteroid after 10 seconds
    Destroy(clone.gameObject, 10);

    Debug.Log ("Asteroid has just spawned");
	
	//Remove this as it does nothing to prevent another asteroid from being spawned
    //yield WaitForSeconds(spawnInterval);
}

EDIT

This script works fine in my project. Please double check that your tags are set correctly and that the prefab is still assigned in the editor.

//Prefab Object
var asteroidPrefab : Rigidbody;
//Array of Spawn Points
private var spawnPointList:GameObject[];
//Variable to hold number of Spawn Points
private var sPLength: int;
//this is the time inbetween each instantiate, you can edit this time in the inspector to make the time between shots longer
public var spawnInterval : int = 3.0;
//this is how fast your cannonball will be fired, change this in the inspector depending on how big your level is
public var asteroidSpeed : int = 100;

function Awake() 
{
	//retrive list of spawn points
    spawnPointList = GameObject.FindGameObjectsWithTag("AsteroidSpawnPoint");
    //Size of Array
    sPLength = spawnPointList.Length;
}

function Start()
{
	InvokeRepeating("AsteroidSpawn", spawnInterval, spawnInterval);
}


function AsteroidSpawn()
{   
    
    //Random spawn point number
    var randomSpawnIndex = Random.Range(0,sPLength);

    //Select random spawnpoint to spawn asteroid
    var asteroidSpawnPosition : Vector3 = spawnPointList[randomSpawnIndex].transform.position;

    //Creates clone of the asteroid prefab
    var clone = Instantiate(asteroidPrefab, transform.position, transform.rotation);

    //Sets random velocity vector
    clone.velocity = Vector3(Random.Range(-100,-200), Random.Range(-100,100), Random.Range(-100,100));

    //Spreads Asteroids
    var randomScalar = Random.Range(100,1500);

    clone.transform.localScale = Vector3.one*randomScalar;

    Debug.Log ("Asteroid has just spawned");

    Destroy(clone.gameObject, 10);
}