Hi, I have been using foreach loop in List for spawning objects but I have an issue, the foreach loops always keep on staying at 1 making making it infinite loop.
private void PortraitBackground() {
float playerPos = player.transform.localPosition.x;
float addBackgroundPos = screenWidth + playerPos;//spawning offset
foreach (var background in currbackground) {
backgroundSize = background.GetComponent<Renderer>().bounds.size;
float backgroundPos = background.transform.position.x;
backgroundEdge = backgroundPos + (backgroundSize.x / 2);
if(addBackgroundPos > backgroundEdge) {
isadd = false;
}
}
if (!isadd) {
AddBackground();
isadd = true;
}
}
This is my instantiating function
private void AddBackground() {
float spawnPos = backgroundEdge + (backgroundSize.x / 2);
backgroundObject = (GameObject)Instantiate(backgroundObject);
backgroundObject.transform.position = new Vector2(spawnPos, 0);
currbackground.Add(backgroundObject);
}
I can see that my currbackground gets filled up its just that var background
keeps staying at 1.