For loop with instantiate crashing unity

I’m trying to use for loop as you can see in the code below, unity is crashing every time when i try to build this. Can anybody explain me why its crashing? :smiley:

I want this loop to be infinite.

GameObjects are set in Unity Editor.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MapManager : MonoBehaviour {
    
        public GameObject mainObject;
    
        [Header("Map elements")]
        public GameObject[] elements;
    
        private int index;
    
        private float timer;
        private float timeLimit;
    
    	void Start () {
    		
    	}
    	
    	void FixedUpdate () {
            timer += Time.fixedDeltaTime;
    
            Debug.Log("Timer: " + timer);
    
            if (timer >= 2f)
            {
                for (int i = 1; i >= 0; i++)
                {
                    index = Random.Range(0, elements.Length);
                    Instantiate(elements[index], new Vector3(0, 20 * i, 0), Quaternion.identity, mainObject.transform);
                }
                timer = 0;
            }
    
    	}
    }

Your for loop runs infinitely, as i will always be greater than or equal to 0. So yes, you’re going to crash as it will spawn objects until you probably run out of memory.

if (timer >= 2f)
{
for (int i = 1; i >= 0; i++)
{
index = Random.Range(0, elements.Length);
Instantiate(elements[index], new Vector3(0, 20 * i, 0), Quaternion.identity, mainObject.transform);
}
timer = 0;
}

Your unity crash because your loop is infinite. >= 0 , I supose you need this.

if (timer >= 2f)
             {
                 for (int i = 0; i <elements.Length; i++)
                 {
                     index = Random.Range(0, elements.Length);
                     Instantiate(elements[index], new Vector3(0, 20 * i, 0), Quaternion.identity, mainObject.transform);
                 }
                 timer = 0;
             }