HELP, How to stop infinite loop in my Script?
Hi guys, iam newbie here. I need some help from you Guys, i have made script for control spawn point and i wanna make the spawn point always summon the object, but i got problem with infinite loop, this made my Editor Not Responding. i cant thinking anymore for this case, so i need some help in this forum. so this is my code.
void Start () {
waktu_spawn = 2f;
delay_spawn = 3f;
InvokeRepeating("Meteorid", waktu_spawn, delay_spawn);
Random1 = Random.Range (1,4);
Random2 = Random.Range (1,4);
}
public void Meteorid () {
for (int i=1; i > 0; i++){
if (Random1 == 1){
StartCoroutine(tunggu());
Debug.Log("1");
}
if (Random1 == 2){
Debug.Log("2");
StartCoroutine(tunggu());
}
if (Random1 == 3){
Debug.Log("3");
StartCoroutine(tunggu());
}
}
if (stopspawn) {
CancelInvoke("Meteorid");
}
}
IEnumerator tunggu(){
yield return new WaitForSeconds (2);
}
}
Thanks
Answer by rainChu · Aug 19, 2018 at 09:12 PM
The cause of your immediate problem is your for loop. It never ends because of how for loops work:
Pseudocode:
for ( an initializer that's done once at the start; an expression to check at the beginning of every loop; an expression to perform at the end of every loop )
First part of your loop is int i = 1
. The next part is your expression to check. You're testing i
to see if it's greater than 0. It always is. No matter how much you add to it, it will always be greater than 0. (Actually, you will eventually have an integer overflow, so this loop will technically end, but that's for another topic entirely...)
Removing the for loop will solve this immediate problem, but it doesn't fix some other things. Firstly, that coroutine doesn't yet do anything. It waits two seconds. Are you sure it needs to be there? Your meteoroid method is already being called on a delay. Unless you have some other timed events that need to happen with each call, you don't need a coroutine to do it.
Also, your call to Random only happens once. This means you'll get two random numbers in Start, and they'll stay the same each call to Meteor()
. To fix this, move the calls to Random.Range() into Meteoroid()
.
Be sure stopSpawn
is eventually set to true! Good luck!
The expression is checked at the beginning of each loop cycle, not at the end. The 3rd part is executed at the end of a cycle. A for loop can be represented by a while loop like this:
for(int i = 0; i < 5; i++)
{
// do something
}
is equivalent to:
{
int i = 0;
while (i < 5)
{
// do something
i++;
}
}
Answer by JVene · Aug 19, 2018 at 09:05 PM
Pause and reflect on what this says:
for (int i=1; i > 0; i++)
As long as i is positive, this will not stop. Eventually i will 'wrap', which is to say that because it is an int, it will reach the limit of a positive integer and suddenly become negative. At that point the loop will stop. Where an int represents a signed 32 bit integer (is at does in C#), this will loop around 2 billion times before it stops, which will take a long while in this context.
So, how many times did you want this loop to repeat? Consider that number instead of zero, and make the test < instead of >