For Loop not working
My for loop is not working and after checking Unity documentation and forums I have not found a solution. Here is the code:
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
public int numPlat;
public GameObject PlatformToSpawn;
// Use this for initialization
void Start () {
Debug.Log("Number" + numPlat);
spawnStuff();
}
// Update is called once per frame
void Update () {
}
public void spawnStuff()
{
for (int i = 0; i > numPlat; i++)
{
Debug.Log("ds");
}
}
}
Looks like your itteration logic is backwards.
try;
for (int i = 0; i <= numPlat; i++)
this will itterate from 0 to whatnever number numPlat happens to be.
So, having this would not work? $$anonymous$$ust the condition always be smaller than the variable? $$anonymous$$any thanks for answering.
If you want to count up, from something to something greater, then this is how you write the logic.
What you had was the itterator testing to see if it was greater than your variable (which you never set as anything so it was always zero anyway). So that condition meant it could never enter the loop.
Answer by Ali-hatem · Apr 25, 2016 at 03:06 PM
for (int i = 0; i < numPlat; i++) you need <sign not >sign
That logic will finish the loop 1 itteration short. You need to have <= to ensure that you're going all the way to the variable value.
@Riderfan how is that if numPlat = 10
loop will keep ruining until i
is not < numPlat
any more & because of increasing i
the loop will stop after 10 times otherwise the loop will run to infinity & the reason i used i< numPlat
not i<= numPlat
because the zero will be count & if you want to instantiate 10 objects then with i<= numPlat
will instantiate 11 objects so with simple debug every thing will be clear
int numPlat = 10;
void Start(){
for(int i = 0; i<numPlat; i++){
print (i);
}
}
the result will be printing the i
teen times from 0 to 9 @upariver hop this will explain every thing to you;
So is it a rule that i must never be higher than the var, otherwise it doesn't run? $$anonymous$$any thanks for answering.
Your answer
![](https://koobas.hobune.stream/wayback/20220612075603im_/https://answers.unity.com/themes/thub/images/avi.jpg)