How to create an endless track using an array of prefabs ?

Hi guys!

First of all, sorry for my bad english.

I’m creating an endless Runner. I followed this tutorial Runner, a Unity C# Tutorial
Now I want to generate the track with a number of different level tiles, which randomly spawn in a row.

With the tutorial I did a Manager that uses only one prefab. So I think I need an array of prefabs which are randomly initiating to the track. But I don’t know how to do it

For the Array I wrote:

var levelTiles : GameObject[];
var levelTile1: GameObject;    
var levelTile2: GameObject;
var levelTile3: GameObject;

levelTiles[0] = levelTile1;    
levelTiles[1] = levelTile1;
levelTiles[2] = levelTile2;

BUT I don’t what I ave to do now.

Can something please help me ? I’m totally new at this.

You can simply use:

public var levelTiles : GameObject[];

You will then be able to populate that array in the Inspector by setting the size of your array and drag prefabs into each element.


Then choose randomly like so:

//Choose new tile, assuming there is at least 1 item in array
var randomTileIndex : int = Random.Range(0, levelTiles.Length);
var myRandomTile : GameObject = levelTiles[randomTileIndex];

It works perfect for me :smiley:
And it’s so simple
Thanks !!