- Home /
Duplicate Question
Where to run InvokeRepeating?
I would like to invokerepeating and instantiate some prefabs based on the current (and changing) value of BallController.score
void Start(){
if (BallController.score>100)
{
InvokeRepeating("Blockers", 1f, 1f);
}
}
void Blockers() {
if (BallController.score > 100)
{
Instantiate(MakinBlockers);
}
}
Currently my code only starts InvokeRepeating("Blockers") if the game is stopped and started over again with a score of more than 100. Where do I need to put InvokeRepeating to make it check the score more often?
the simple answer is YES, START IS AN O$$anonymous$$ PLACE TO PUT IT. that's settled. secondly remove the if line from Start, as Robert tells you to. that's settled. this question is now answered. if you have more problem ask a new question.
I asked where I can put InvokeRepeating to check on it more often, not whether I can put it in start.
Also, I didn't upvote because I don't have enough reputation, I can only mark reply's as answers and I didn't understand yet whether it was a full answer.
I made a forum post incase anyone else needs help with this in the future: http://forum.unity3d.com/threads/247766-How-to-Regularly-Check-on-an-Int-from-another-Script
Answer by robertbu · May 23, 2014 at 04:45 PM
If you are doing what I think you are, then you need to remove this condition in Start():
if (BallController.score>100)
That is, you want InvokeRepeating() to be called all the time, bu you only Instantiate() if the score is > 100.
Robertbu, thanks for the reply, I was trying to condense my problem to the purest question, but I think I maybe didn't leave a clear idea of what I'm trying to accomplish.
$$anonymous$$y goal is to use the if operator for both InvokeRepeating and Instantiate because I want to specify the intervals as well as instantiating different Prefabs of $$anonymous$$akinBlockers based on BallController.score.
$$anonymous$$y best guess is that I simply shouldn't be placing the InvokeRepeating in Start() but all places I've tried moving haven't worked yet.
Here is a more detailed example of my script:
void Start() {
if (BallController.score >= 1001 && BallController.score <= 3000)
{
InvokeRepeating("Blockers", 1f, 1f);
}
if (BallController.score >= 3001 && BallController.score <=8000)
{
InvokeRepeating("Blockers", 1f, 1f);
}
//this would continue on...
}
void Blockers() {
if (BallController.score >= 1001 && BallController.score <= 3000)
{
Instantiate($$anonymous$$akinBlockers);
}
if (BallController.score >= 3001 && BallController.score <=8000)
{
Instantiate($$anonymous$$akinBlockers2);
}
//this would also continue on
}
If you need to vary the ti$$anonymous$$g, then you don't want InvokeRepeating(). You can use Invoke() and chain the commands, but a coroutine would be a better choice.
void Start() {
StartCoroutine($$anonymous$$yInvokeRepeating());
}
IEnumerator $$anonymous$$yInvokeRepeating() {
while (true) {
yield return new WaitForSeconds(waitTime);
Blockers();
}
}
You need to declare waitTime at the top of the file:
private float waitTime = 1.0f;
Now you can change the wait time between Blockers() calls by changing the value of waitTime.
That doesn't fix the problem I am having, but I went ahead and changed it in the meantime. Thanks, it gives me some new stuff to read about.
$$anonymous$$y problem is still: $$anonymous$$y Prefab isn't Instantiated when score reaches a certain point, but does instantiate on a game restart(you die, (sccore doesn't reset) and restart the game).
I was reading the documentation about OnGUI() this morning and realized that it is calling quite often, in order to update such things, so perhaps I can take advantage of that. I'll let you know what I figure out.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# the position of the object 1 Answer
If gameobject moves do this 1 Answer
if and invoke repeating in start... 1 Answer