How do you make prefabs spawn faster at certain time intervals.

I have been trying to find out how to make my prefabs spawn faster but I have had no success I have attempted to make one but it just makes a ton of errors so here is my regular spawn script I would appreciate any help that I can get with this.

using UnityEngine;
using System.Collections;

public class spawnscript : MonoBehaviour {

public GameObject[] obj;
public float spawnMin = 1f;
public float spawnMax = 2f;

void Start () {
    Spawn();
}

void Spawn() {
    Instantiate (obj[Random.Range (0, obj.GetLength(0))], transform.position, Quaternion.identity);
    Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
}

}

I would personally do something along the lines of the example here. Rather than using self-calling methods. Then you can more simply change the spawnRate, and you get a bunch of nice side-effects like having a real-time count-down etc. It’s a much more powerful base system.

However if you want to stick with self-calling methods. You could simply adjust your code like so (untested):

using UnityEngine; using System.Collections;

public class spawnscript : MonoBehaviour {

 public GameObject[] obj;
 // Spawn at least one enemy every second.
 public float spawnMin = 1f;
 // Spawn at most two enemies every second.
 public float spawnVar = 1f;
 // Increase spawn rate average by 0.1 each second.
 public float spawnInc = -0.1f;
 
 void Start () {
     Spawn();
     IncreaseDifficulty();
 }
 
 void Spawn() {
     Instantiate (obj[Random.Range (0, obj.GetLength(0))], transform.position, Quaternion.identity);
     Invoke ("Spawn", spawnMin + Random.value * spawnVar);
 }
 void IncreaseDifficulty() {
     if (spawnMin + spawnInc > 0) {
         spawnMin += spawnInc;
     }
     Invoke ("IncreaseDifficulty", 1);
 }
}

I also took the liberty to change your randomizing method. Mine just makes life a bit simpler when changing the rate.

Note: With these particular values you’ll probably want to change spawnVar over time too. But you need to do something yourself :wink: