c# spawn array

So I’m trying to get this script to spawn objects on pre defined locations every few seconds.

In my case those are planets, i have prefabs of these which i drag onto the script.

public GameObject[] planets;
private Vector3[] spawnGrid;

void Awake () {

Vector3[] spawnGrid = new [] { new Vector3(12,3,0), 
                               new Vector3(12,2,0), 
                               new Vector3(12,1,0), 
                               new Vector3(12,0,0)};

}
	
void Update () {

   float randomTimer = Random.Range (10, 20);

   randomTimer -= Time.deltaTime;

   if (randomTimer < 0) {
        RandomSpawn ();
   }
}

void RandomSpawn(){

int randomPlanet = Random.Range(0, 7);

Instantiate(planets[randomPlanet], spawnGrid[0], transform.rotation);

}

This is the script.
At the top I’m defining my array of prefabs.

The SpawnGrid are the locations which i want to instantiate the prefabs at.

These should also be randomly chosen but that’s not a problem for now.

What I want to happen in the Update function: Every 10-20 seconds the RandomSpawn function gets called.

Then the RandomSpawn function instantiates a random prefab.

At the moment nothing is happening when the script plays and I’m getting the error that my planets array will always return null.

hello @L0r3nz19, I think you want to generate your planet after some random time.

you get basic idea on Coroutine (which i use here.) ,here ,here
I didnot tested this code.

    public GameObject[] planets; // You want to assign this from inspector.
	private Vector3[] spawnGrid;
	
	void Awake () {
		
		spawnGrid = new [] { new Vector3(12,3,0), 
			new Vector3(12,2,0), 
			new Vector3(12,1,0), 
			new Vector3(12,0,0)};
		
	}
	
	void Start () {
		StartCoroutine ("SpwanPlanetAfterThisTime", getRandomTime ());
	}
	
	void Update () {
		
		float randomTimer = Random.Range (10, 20);
		
		randomTimer -= Time.deltaTime;
		
		if (randomTimer < 0) {
			RandomSpawn ();
		}
	}
	
	IEnumerator SpwanPlanetAfterThisTime(int waitTime ) {
		yield return new WaitForSeconds(waitTime );
		RandomSpawn(); // To spawn planet.
		StartCoroutine ("SpwanPlanetAfterThisTime", getRandomTime ()); // To again spwan other planet after random time.
		//		StopCoroutine ("SpwanPlanetAfterThisTime"); // You can use this when you want to stop spwan more planet. // when game is over or something like that.
		print("WaitAndPrint " + Time.time);
	}
	
	
	void RandomSpawn(){
		
		int randomPlanet = Random.Range(0, 7);
		
		Instantiate(planets[randomPlanet], spawnGrid[0], transform.rotation);
		
	}
	
	int getRandomTime()
	{
		float randomTimer = Random.Range (10, 20);
		return randomTimer;
	}

hope this help. inform me if you have questions.

time.deltatime?

or time.time

Your update function is wacky:

void Update () {
  float randomTimer = Random.Range (10, 20);
  randomTimer -= Time.deltaTime;
  if (randomTimer < 0) {
    RandomSpawn ();
  }
}

That says “On every frame, choose a number between 10 and 20. Then, take away the amount of time that has elapsed since the last frame. If the amount you’ve got left is less than 0, spawn a planet.

This will only ever work if your frame rate is so bad that it takes more than 10-20 seconds to render a frame…