How to generate a random generated endless road

Okay am really new to coding with unity. I basically code in JavaScript but I had to try to understand some piece of code because I wanted to create an endless road which will randomly select between prefabs and create an endless road like temple run.
I want to use this for a competition I need to win so I really need your help Guys please!! Thanks in advance.
Here is the code I attached to an empty game object and assigned a prefab plane with texture and some trees.

using UnityEngine;
using System.Collections.Generic;

public class RandomPlaneGen : MonoBehaviour {

	public Transform prefab;
	public int numberOfObjects;
	public float recycleOffset;
	public Vector3 startPosition;

	private Vector3 nextPosition;
	private Queue<Transform> objectQueue;

	void Start () {
		objectQueue = new Queue<Transform>(numberOfObjects);
		nextPosition = startPosition;
		for (int i = 0; i < numberOfObjects; i++) {
			Transform o = (Transform)Instantiate(prefab);
			o.localPosition = nextPosition;
			nextPosition.x += o.localScale.x;
			objectQueue.Enqueue(o);
		}
	}

	void Update () {
		if (objectQueue.Peek().localPosition.x + recycleOffset < Runner.distanceTraveled) {
			Transform o = objectQueue.Dequeue();
			o.localPosition = nextPosition;
			nextPosition.x += o.localScale.x;
			objectQueue.Enqueue(o);
		}
	}
}

:(The problem is my runner runs infinity like I wanted. But how can I create different planes and make the planes
to be choose randomly between a set of planes I created. I know that is not the best code to do that so i stand corrected.

I added this code to the runner to check his position at runtime.

using UnityEngine;

public class Runner : MonoBehaviour {
	public static float distanceTraveled;

	void Update(){
		transform.Translate(0f,0f,2f*Time.deltaTime);
		distanceTraveled = transform.localPosition.x; // Tells us the current position of the object
	}
}

Thanks Guys.:smile:

public GameObject[ ] planes;

then based on where your player is get a random index between 0 and planes.Length and instantiate the plane at that random index at the needed position.

Then in the editor you can assign all of your planes to the planes variable.

Sounds like maybe you could just create the planes in the scene, hide them. Create some public variables in your script ie var planes:int[ ] = new int[10] or whatever… to expose them in the inspector. Drag-drop the planes onto the array elements. Then use Random.value() or Random.Range() to randomly choose an index in the array, and do something with that plane - position it or whatever.

You said you code in javascript but your code is c#? lol

Lol, pls don’t mind me I have 2 unity accounts. Yea I code in Java script but understand C# if I focus on the codes. Okay I watched the video tutorial from unity and I really learnt alot from the mechanics used to generate and destroy game objects. And yeah it was pretty simple spawning plan ground in which the player runs in randomly. Thanks alot. And another challange I have right now is how to generate an array of coins. The tutorial genretes a single cone at a time. How do I make them flow through a pattern and remain organised. Any suggestions guys. Thanks for the quick response. I promise you all one cow from Nigeria when I win the competition. Lol hope you know how to kill one.

Please be fair enough and help me out with the lines of code. I know they are many ways to achieve the endless terrain concept. When u say I should create an array and add the planes while I call the prefabs randomly Pls how do I that. Codes Pls I wouldn’t ask if I knew it. Thank you.