- Home /
Help With "For Loop" Not Working?
I have a "for loop" which I want to execute the code inside every time a "SpiderAmount" is added. Here it is:
for (var i = 0; i < SpiderAmount; ++i)
{
Instantiate (spiderPrefab, Vector3 (2.0, 0, 0), Quaternion.identity);
print (i);
}
I put it in an "Update function" and it printed i forever and kept on printing. It printed 0, 1, 0, 1 constantly. I must be doing something wrong, do you know what?
Answer by Dracorat · Nov 13, 2013 at 05:50 PM
Well, by virtue of being in the Update function, it means you're running it every frame. So, every frame you're spawning (for now) two spiders.
Shouldn't you have some condition that causes this to happen? Like a timer or a collision?
The condition that causes it to happen is how many "SpiderAmount" there is? I put it in an "if statement" when the L mouse button is pressed but it still went on forever?
In this case please include all the code, including both spider amount and button press checks.
Answer by Lovelock · Nov 13, 2013 at 07:40 PM
Try adding one line inside your for loop if you want to do it this way:
for (var i = 0; i < SpiderAmount; ++i)
{
Instantiate (spiderPrefab, Vector3 (2.0, 0, 0), Quaternion.identity);
print (i);
SpiderAmount--; // Reduce the amount since we just spawned a spider.
}
There are definitely different approaches, but this should fix your for loop.
FYI, this will only spawn half the desired number of spiders since your condition is < SpiderAmount
, you have an incrementer: ++i
AND you decrement the SpiderAmount: SpiderAmount--
(That last should not be there with the first two.
Just pasted the code into a basic example and it spawned the proper number of spiders. I even made SpiderAmount increase from user input and it continued spawning the proper number. Of course modifying the condition of a for loop inside of a running loop is not the ideal approach.
Another option is to set
SpiderAmount = 0;
directly after the for loop.
Perhaps something like the following would be a more efficient approach altogether: (Forgive me for writing it in C#)
void Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
SpiderAmount = 2;
}
if(SpiderAmount > 0)
{
SpawnSpiders();
}
}
void SpawnSpiders()
{
for (int i = 0; i < SpiderAmount; ++i)
{
Instantiate(spiderPrefab, new Vector3 (Random.Range(-2.0f,2.0f), Random.Range(-2.0f,2.0f), 0f), Quaternion.identity);
print (i);
}
SpiderAmount = 0;
}
Wrap it in a nice spawning function and reset the variable once spawning is complete. :)
This was an arbitrary example to get it to spawn a prefab when you press the space key. In your program, you can modify the SpiderAmount variable however you like. $$anonymous$$aking the variable any non-zero value will spawn that number of spiders that frame.
Your answer
Follow this Question
Related Questions
Using for ... var ... in loop to access variable. 1 Answer
x=x Assignment made to same variable 3 Answers
Using for as while. 3 Answers
Can't instantiate in loop - crashes unity 1 Answer
For Loop inside While Loop won't Repeat 0 Answers