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
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.
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 :)
Answer by Hellium · May 01, 2019 at 08:39 AM
private List<Renderer> instances = new List<Renderer>(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[i].sortingOrder = Mathf.Max( 0, renderers[i].sortingOrder - 1 );
}
Hello,
Thanks the help with the array :) it works!
As for what I meant with the 2nd problem.
Thanks to your help, it looks like this when spawning. there are some cases it overlaps but not all which breaks the connecting effect.
I want the last object that spawns be always on top of the previous object so it would seem connect like these. The problem with this approach is it is just one game object and not an array of prefabs. I can't use the sorting order because the last object in my array won't connect properly with the first one in my array. Sorry if it's hard to understand, the main point is just have the last spawned object in my array to be on top of the previous object spawned.
Thanks again so much for your help :)
I've edited my answer to add a way to change the sorting order of all the instantiated obstacles. Give it a try, but I haven't tested the code myself.
Thanks,
I tried using your updated answer.
there were 2 problems that I noticed.
1st that it spawned 2 gameobjects from the array at the same time, but the last spawn always stayed on top of the previous spawn.
2nd problem is that your script conflicted with the destroyer script of $$anonymous$$e, which is made to destroy the game objects after a given time. Since this will be an endless runner, I put a lifespan on my game objects that go out of screen.
Thanks again for continuous help. Very appreciated :)
Answer by Lionesse · Nov 25, 2020 at 11:09 PM
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.
Your answer
Follow this Question
Related Questions
How do I spawn a certain amount of objects randomly between 2 positions? 0 Answers
Trying to spawn enemies on only one path 1 Answer
Spawning limited GameObjects at a specific position not working 1 Answer
Why do I keep getting 'The object you want to instantiate is null' warning? 1 Answer
Spawn prefabs from array in specific order given the size of prefab 1 Answer