Change InvokeRepeating interval based on amount of game runtime
Hi all, first post,
So, I'm going through some Udemy tutorials on Unity and I'm loving it so far. What I'm working on at the moment is a simple "bomb clicker" game (2D). The premise is simple: bombs fall from the sky, and you have to click them to destroy them before they hit the ground.
Thanks to the tutorial, I have the basic game mechanics down. Bombs fall in a random X range, and between two boundaries, you can click to destroy them, incrementing a counter for how many you hit, and if a bomb gets past the lower boundary and the bomb hits the ground, a counter gets incremented for the one you missed.
This game uses a method within the Start () function called "InvokeRepeating" to make the bombs fall every 1 second right now.
So, here's what I would like to happen, but I am a newb in the coding side of things and I'm failing to get it to work: While the game runtime (how much time has elapsed since you clicked play) is less than 15 seconds, I want bombs to fall at the normal rate of 1 per second. Once 15 seconds have elapsed, I want them to fall at a rate of 1 every half a second, which would imply to me that I just need to adjust the repeatRate of the InvokeRepeating method to 0.5. Then, once 30 seconds have passed, I want bombs to drop even faster. You get the idea. Bombs continually fall faster and faster until you can't click enough. Essentially, once you have missed more than you have hit, I want the game to end. Your score is how long you last.
So, how can I get the InvokeRepeating method to adjust the repeat rate based on how long the game has been running? I've tried while loops, counters, etc. But with the InvokeRepeating method in the Start function, it doesn't seem that I can adjust the repeat rate once the game has started. Any help would be appreciated! Here's my "InstantiateGobj" script:
#pragma strict
public var gObj : GameObject;
var elapsed : float;
var fps : float;
function Start () {
InvokeRepeating("CreateRandom", 0, 1);
}
function CreateRandom () {
var x : float;
var y : float;
x = Random.Range(-6.25, 6.3);
y = 6;
Instantiate( gObj, Vector3(x,y,0), Quaternion.identity);
}
function Update (){
elapsed += Time.deltaTime;
//fps = 60 / Time.deltaTime;
//Debug.Log("Your FPS: " + fps);
Debug.Log(elapsed);
}
Answer by LazyElephant · Dec 27, 2015 at 07:46 AM
The answer is to use Invoke instead of InvokeRepeating. You can simulate the InvokeRepeating functionality by adding another Invoke inside the invoked method.
function Start () {
Invoke("CreateRandom", 1);
}
function CreateRandom () {
var x : float;
var y : float;
x = Random.Range(-6.25, 6.3);
y = 6;
Instantiate( gObj, Vector3(x,y,0), Quaternion.identity);
Invoke("CreateRandom", WhateverAmountYouWant);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612054009im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity crashes when using while loop 0 Answers
how to display object while ! raycast hit is true ? 2 Answers
Consistent spacing between gameobjects when some disabled 1 Answer
Stucked in a infinite loop and don't know why 2 Answers
Looking for advice about how to streamline my code (avoid repetition) 0 Answers