- Home /
Question by
jamesorion44 · Oct 18, 2020 at 10:46 PM ·
scripting problem3dgenerationendless runnercar game
Tile generator only spawns one tile.
I'm making an endless drifting game with road that generates forever. I have this script below that does the actual generating but it only spawns the same prefab over and over again. How can I make it spawn the other two prefabs that I have in my list. Thanks.
using UnityEngine;
public class TileManager : MonoBehaviour {
public GameObject[] tilePrefabs;
private Transform playerTransform;
private float spawnZ = -35.0f;
private float tileLength = 2.8f;
private int amnTilesOnScreen = 10;
private float safeZone = 9.0f;
private List<GameObject> activeTiles;
// Start is called before the first frame update
void Start()
{
activeTiles = new List<GameObject>();
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
for (int i = 0; i < amnTilesOnScreen; i++)
{
SpawnTile();
}
}
// Update is called once per frame
void Update()
{
if (playerTransform.position.z - safeZone > (spawnZ - amnTilesOnScreen * tileLength))
{
SpawnTile();
DeleteTile();
}
}
private void SpawnTile(int prefabIndex = -1)
{
GameObject go;
go = Instantiate (tilePrefabs[0]) as GameObject;
go.transform.SetParent (transform);
go.transform.position = Vector3.forward * spawnZ;
spawnZ += tileLength;
activeTiles.Add(go);
}
private void DeleteTile()
{
Destroy(activeTiles[0]);
activeTiles.RemoveAt(0);
}
}
Comment
Your answer
Follow this Question
Related Questions
Tiles spawn slightly to the right of my player 1 Answer
Help with AI car physics 1 Answer
FPS recoil 1 Answer
How do I make a spotlight make a model play an animation? 2 Answers
Accurate Projectile Leading for AI [Third person shooter] 0 Answers