Update / Frame question

Hi,

I’m brand new to Unity and really loving in. Fantastic platform!

I do have a very newbish question here, and it’s related to how Update is called per frame. Basically I have a simple for loop to do some testing, and within this loop I spawn a prefab. However I am noticing that each iteration of the loop seems to get called every other frame, which seems odd to me. Here is the simple code:

// Update is called once per frame
	void Update () {
		
		if(spawnObjects)
		{
			
			for(int n=0;n<10;n++)
			{
				SpawnIt("Hello " + n.ToString());
				Debug.Log("Called " + n.ToString());
				n++;
			}
			spawnObjects =false;
			
		}
		
	}

What gets printed to the log is:

etc.

I rather expected it would be “Called 0, Called 1, Called 2”.

What am I missing here?

Thanks!

Don’t increment n++ at the end of your loop. In the for loop statement you do the increment already.

for(int n=0;n<10;n++)

The last section (n++) is what happens at the end of the loop. By incrementing it again within the loop you end up skipping numbers.

D’oh! That has to be the most newbish mistake I’ve made in quite some time, jeez!

Thank you :slight_smile: