Object spawn percentage?

I’m trying to make a game that spawns a long hallway. I have five identical hallway/gameobjects, and i’m trying to make is so certain ones of a less chance of spawning. I currently have it so the starting hallway has a spawner at the end, which spawns another hallway, which goes on and on, but then the layout is the same. Is there a way a script could have it had a chance of spawning each room?

You could have an array with the five prefabs and another array with the chances for each one to spawn, and use a script like this:

```var prefabs: GameObject[]; // set the array size and drag the prefabs here
var chances: int[]; // set the same array size and define the chances for each one

function RandomSpawn(pos:Vector3, rot:Quaternion){
var rand = Random.Range(0, 100); // draw a random int in 0..99 range
var rangeStart = 0;
for (var n: int = 0; n < prefabs.length; n++){
var rangeEnd = rangeStart + chances[n]; // calculate this object's chance range
if (rand >= rangeStart && rand < rangeEnd){ // if random number inside the range...
Instantiate(prefabs[n], pos, rot); // create the corresponding prefab
}
rangeStart = rangeEnd; // next range right after the current one
}
}

// Example: create 10 random sections at Start; each section has length 5, and
// the first one will be created at this object's position (assuming this script
// is attached to an empty object):

function Start(){
var position = transform.position; // start at this object's position
var rotation = transform.rotation; // keep this object's orientation
for (var n = 0; n < 10; n++){
RandomSpawn(position, rotation);
pos += 5 * transform.forward; // advance position 5 units ahead
}
}
```

Some important notes:

1- This script must be attached to a separate object (the terrain, the camera, an empty object etc.)

2- chances and prefabs must have the same length, of course;

3- The chance for each object is expressed in percentage - like 20, 10, 30, 40, for instance. Obviously, the sum of all chances must be equal to 100.

EDITED:
Added example to show how to use RandomSpawn.

Here is @aldonaletto 's code converted to C# with formatting.

``````using UnityEngine;

public class RandomSpawnScript : MonoBehaviour
{
// Set the array size and drag the prefabs here.
public GameObject[] prefabs;

// Set the same array size and define the chances for each prefab.
public int[] chances;

void RandomSpawn(Vector3 pos, Quaternion rot)
{
// Draw a random int in 0..99 range.
int rand = Random.Range(0, 100);
int rangeStart = 0;
for (int n = 0; n < prefabs.Length; n++)
{
// Calculate this object's chance range.
int rangeEnd = rangeStart + chances[n];

if (rand >= rangeStart && rand < rangeEnd)
{
// If random number inside the range,
// create the corresponding prefab.
Instantiate(prefabs[n], pos, rot);
}

// Next range right after the current one.
rangeStart = rangeEnd;
}
}

// Example: create 10 random sections at Start; each section has length 5, and
// the first one will be created at this object's position (assuming this script
// is attached to an empty object).
void Start()
{
// Start at this object's position.
Vector3 position = transform.position;

// Keep this object's orientation.
Quaternion rotation = transform.rotation;

for (int n = 0; n < 10; n++)
{
RandomSpawn(position, rotation);