increase difficult every 30 seconds by spawning car more faster

Hello I’m new in unity and c#, I have a big problem when I create my first project create 2D racing game. My problem is I want to increase the difficult every 30 seconds. so every 30 seconds enemy car spawning more faster. I’m stuck here.
Here’s my scripts :

using UnityEngine;
using System.Collections;

public class EnemySpawner : MonoBehaviour 
{
	public GameObject[] EnemyCars;

	int EnemyCarNo;

	float maxSpawnRateInSeconds = 4f;

	// Use this for initialization
	void Start () 
	{

	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}

	//Function to spawn an enemy 
	void SpawnEnemy()
	{
		//this is the bottom-left of the screen
		Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (0, 0));
		//this is the top-right point of the scren
		Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1));

		//Instantiate an enemy
		GameObject anEnemy = (GameObject)Instantiate (EnemyCars[EnemyCarNo]);
		anEnemy.transform.position = new Vector2 (Random.Range (min.x, max.x), max.y);
		EnemyCarNo = Random.Range (0, 7);

		//Schedule when to spawn next enemy
		ScheduleNextEnemySpawn ();
	}

	void ScheduleNextEnemySpawn ()
	{
		float spawnInNSeconds;
		if (maxSpawnRateInSeconds > 1f) {
			//pick a number between 1 and maxSpawnRateInSeconds
			spawnInNSeconds = Random.Range (1f, maxSpawnRateInSeconds);
		} else
			spawnInNSeconds = 1f;
		Invoke ("SpawnEnemy", spawnInNSeconds);
	}


	void IncreaseSpawnRate()
	{
		if (maxSpawnRateInSeconds > 1f) 
			maxSpawnRateInSeconds--;
		if (maxSpawnRateInSeconds == 1f)
			CancelInvoke ("IncreaseSpawnRate");

	}

	//Function to start enemy spawner
	public void ScheduleEnemySpawner()
	{
		Invoke ("SpawnEnemy", maxSpawnRateInSeconds);
		
		//Increase spawn rate every 30 seconds
		InvokeRepeating ("IncreaseSpawnRate", 0f, 0.2f);
	}
	//Function to stop enemy spawner
	public void UnscheduleEnemySpawner()
	{
		CancelInvoke("SpawnEnemy");
		CancelInvoke("IncreaseSpawnRate");

	}
}

Sorry for my English

Best way to do that would be to use a Coroutine and WaitForSeconds.
Create a Coroutine (with IEnumerator) that will increase the difficulty after 30 seconds and that calls itself again as soon as it’s finished, so that you have a 30 seconds cycle as intended.

    [SerializeField]
    private float _intervalBetweenSpawRateIncreasesInSeconds = 30f;

    private IEnumerator ScheduleIncreases()
    {
        // Wait for 30 seconds
        yield return new WaitForSeconds(_intervalBetweenSpawRateIncreasesInSeconds);
        // Increase difficulty
        IncreaseSpawnRate();
        // And reschedule the next increase in 30 seconds
        StartCoroutine(ScheduleIncreases());
    }

Then you just have to start this cycle.

    void Start()
    {
        StartCoroutine(ScheduleIncreases());
    }

Thankyou so much it works. That help me a lot :slight_smile:

@corn can u helped me with this i want my enemy cars gets spawn faster and faster
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class carspawner : MonoBehaviour {
public GameObject Cars;
int CarNo;
public float maxpos = 2.161074f;
public float delaytimer =1f;
float timer;

// Use this for initialization
void Start () {
    timer = delaytimer;
}

// Update is called once per frame
void Update () {
    timer -= Time.deltaTime;
    if (timer <= 0) {
        Vector3 carpos = new Vector3(Random.Range(-2.161074f, 2.161074f), transform.position.y, transform.position.z);
        CarNo = Random.Range(0, 5);
        Instantiate(Cars[CarNo], carpos, transform.rotation);
        timer = delaytimer;
    }
    
}

}