Hey everyone, I’m new to coding. It seems like people are very nice and helpful on here, so I thought I’d ask a question I’ve been stuck on for a long time now. I’m currently making a 2D game where balls are dropping from the top of the screen to the bottom of the screen. Your goal is to pick them up, and if you let one fall on the ground, you lose. The spawn rate is set to 2 sec. I want this increased by 0.1 sec every 30 seconds (2 sec, 1.9 sec, 1.8 sec, etc.). I feel like I’m close, but I can’t seem to get it to work. Here is the code I’m working on. Tell me, how can I make this happen? Thank you in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnBallsNew : MonoBehaviour
{
public GameObject fallingObject;
float delayAndSpawnRate = 2;
float timeUntilSpawnRateIncrease = 30;
void Start()
{
InvokeRepeating("SpawnObject", delayAndSpawnRate, delayAndSpawnRate);
StartCoroutine(ScheduleIncreases());
}
void SpawnObject()
{
float x = Random.Range(-11, 11);
Instantiate(fallingObject, new Vector2(x, 7), Quaternion.identity);
}
IEnumerator ScheduleIncreases()
{
yield return new WaitForSeconds(timeUntilSpawnRateIncrease);
IncreaseSpawnRate();
StartCoroutine(ScheduleIncreases());
}
void IncreaseSpawnRate()
{
if (delayAndSpawnRate > 1)
{
delayAndSpawnRate -= 0.1f;
}
}
}