- Home /
How to create a faster and faster function call?
I have a script that creates a coin at rate seconds and every rate seconds, rate is decreased by fasterRate. This should create the coins faster and faster, but the InvokeRepeating() called with rate is only called at start so that it doesnt get faster and faster but just stays the same rate. I need a way to call CreateCoin() faster and faster. Thanks!
#pragma strict
var xpos : float;
var spawnpoint : Vector3;
var coin : GameObject;
var rotation : Quaternion;
rotation.eulerAngles = new Vector3(0, 0, 0);
var newCoin :Object;
var rate : float = 3.0;
var fasterRate : float = 0.1;
function Start () {
InvokeRepeating("CreateCoin", 1, rate);
}
function Update () {
Destroy(GameObject.FindWithTag("Coin"),10);
}
function CreateCoin(){
xpos = Random.Range(-50.0,50.0);
spawnpoint = new Vector3(xpos, 80, 0);
var myColor : Color; // This is just working for another part of my script, Please Ignore.
var mode : boolean = Random.value > 0.5f;
var rand : float = Random.value;
var index : int = Random.Range( 0, 3 );
myColor[index] = rand;
myColor[( index + ( mode ? 1 : 2 ) ) % 3] = 1;
myColor[( index + ( mode ? 2 : 1 ) ) % 3] = 0;
newCoin = Instantiate(coin, spawnpoint, Quaternion.identity);
//newCoin = newCoin as GameObject;
//newCoin.renderer.material.color = myColor;
rate = rate - fasterRate;
}
I'm a big fan of StartCoroutine and IEnumerator for this kind of thing. Lets you have a lot of control of exactly when things should occur.
Seems there are some differences with C# and JavaScript for this stuff so hopefully those work in JavaScript too.
There's no differences with C# and Javascript with the use of coroutines, only syntax.
Answer by robertbu · Feb 06, 2014 at 05:06 AM
Rather than use InvokeRepeating(), just use Invoke() at the end of CreateCoin(). Insert between line 36 and 37:
Invoke("CreateCoin", rate);
and in Start():
Invoke("CreateCoin", rate);
I'd be a little concerned about using recursion unnecessarily, if it even works with Invoke (haven't tested).
@Eric5h5 - I agree your solution is better, though chaining Invoke() does work. To me this is not recursion since the code returns from CreateCoin() before the next Invoke() is executed.
This seems like the best option for me, and after some testing, works great. Thanks!
Answer by Eric5h5 · Feb 06, 2014 at 05:12 AM
It's simplest to use a coroutine in a loop, along with WaitForSeconds:
function Start() {
while (true) {
CreateCoin();
yield WaitForSeconds(rate);
rate -= .1;
}
}
Although you'd want to put in some condition for making rate not go too low.
Answer by NDJoshi · Mar 31, 2014 at 01:59 PM
I think you should use coroutine();
And pass a variable in hold time(yield). In Update() decrease this variable.
Try this and you will get the exact result you want. If is doesn't, reply me here.
There's no reason why you would use Update at all. See my answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132653im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to use functions between two scripts 2 Answers
OnCollisionEnter does not work. 1 Answer
Performance Optimization ~Function Update: Loop or Once ? 5 Answers
Restart this script 3 Answers
Camera Move 1 Answer