Can I spawn with an array in order?

Hello,

I’m currently making a 2d endless runner game.
For my endless background, i’m doing a spawner using an array, that way I could add more objects in the future and it’s easier for me to manage.

I’m currently having 2 problems with this method.


1st is the script I got from tutorials, spawns the objects in my array “Randomly” however I want my objects to be spawned in order of the array. So that i could lessen the chances of the same object spawning twice next to each other.

here’s the script that I used

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawner : MonoBehaviour
{

    public GameObject[] obstaclePatterns;

    private float timeBtwSpawn;
    public float startTimeBtwSpawn;
    public float decreaseTime;
    public float minTime = 0.65f;

    private void Update()
    {
        if (timeBtwSpawn <= 0)
        {
            int rand = Random.Range(0, obstaclePatterns.Length);
            Instantiate(obstaclePatterns[rand], transform.position, Quaternion.identity);
            timeBtwSpawn = startTimeBtwSpawn;
            if (startTimeBtwSpawn > minTime)
            {
                startTimeBtwSpawn -= decreaseTime;
            }
            
        }
        else
        {
            timeBtwSpawn -= Time.deltaTime;
        }
    }




}

and it looks like this
alt text


2nd Problem is that I want these objects to overlap each other. I tried sorting the layers but since it spawns randomly, it looks messed up even more. Also since this would be a repeating pattern, I was hoping for a solution that the last object spawned is always on top to create a connecting effect.

alt text

Sorry if these problems may seems too much to ask. I’ve spent days researching and trying to find a solution. But since this is my first game project and my first time using unity, it’s been hard finding for answers.

Hopefully someone helps me out with this. It’ll be much appreciated :slight_smile:

private List instances = new List(16);

 public int maxObstaclesCount = 10 ;
 private int lastInstantiatedObjectIndex = -1;
 private void Update()
 {
     if (timeBtwSpawn <= 0)
     {
         SpawnNextObstacle();
         timeBtwSpawn = startTimeBtwSpawn;
         if (startTimeBtwSpawn > minTime)
         {
             startTimeBtwSpawn -= decreaseTime;
         }
         
     }
     else
     {
         timeBtwSpawn -= Time.deltaTime;
     }
 }

 private void SpawnNextObstacle()
 {
         lastInstantiatedObjectIndex = (lastInstantiatedObjectIndex + 1) % obstaclePatterns.Length;

         // Get the renderer to change its sorting order
         Renderer renderer = Instantiate(obstaclePatterns[lastInstantiatedObjectIndex ], transform.position, Quaternion.identity).GetComponent<Renderer>();

         // Destroy oldest obstacle and decrease by 1 the sorting order of all the instantiated renderers
         if( instances.Count >= maxObstaclesCount )
         {
             GameObject oldObstacle = instances[0].gameObject;
             instances.RemoveAt( 0 ) ;
             Destroy( oldObstacle ) ;
             DecreaseSortingOrder( instances ) ;
         }

         // Change sorting order of newly instantiated object
         renderer.sortingOrder = instances.Count;
         instances.Add( renderer  );
 }

 private void DecreaseSortingOrder( List<Renderer> renderers )
 {
      for( int i = 0 ; i < renderers.Count ; ++i )
           renderers_.sortingOrder = Mathf.Max( 0, renderers*.sortingOrder - 1 );*_

}

Why so complicated? You can use while loop inside IEnumerator to increase index number every second (or whatever value you would like) and use that number as function’s parameter.
I suppose you already have rather simple object pooling script that take cares of garbage collection anyway.