Hello, I am quit a noobi in unity and coding in general, so I am sorry for my lack of knowledge. But currently I wanted to make an infinite runner, I have the more or less most things I need to have in the game, but I am still missing the endless part of the runner. I did try to make a script, it wasn’t anything great but it was something, its spawned prefabs in front of the character as he went forward. But the only problem I have is that I just doesn’t seem to be able to have multiple prefabs randomly spawn in front of the player. it instead only takes the first prefab and always used that, can you see the problem with my horribly written script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GroundManagerScript : MonoBehaviour {
public GameObject[] GroundPrefabs;
private Transform playerTransform;
private float spawnZ = 50f;
private float groundLength = 50f;
private int amnGroundsOnScreen = 5;
private void Start ()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
for (int i = 0; i < amnGroundsOnScreen; i++)
{
SpawnGround();
}
}
private void Update ()
{
if (playerTransform.position.z > (spawnZ - amnGroundsOnScreen * groundLength))
{
SpawnGround();
}
}
private void SpawnGround(int prefabIndex = -1)
{
GameObject go;
go = Instantiate (GroundPrefabs[0]) as GameObject;
go.transform.SetParent (transform);
go.transform.position = Vector3.forward * spawnZ;
spawnZ += groundLength;
}
}