Random.Range not giving random numbers?

Hello,
I’m working on a randomly generated level where the player hits an invisible object, random.range makes a number between 1 and 8, and then one of 8 prefabs is placed infront of the player. But for some reason I’m only getting 3 of those 8 prefabs.

Update:
I had a guitext show me the random numbers, its generating 1 through 8 after all but even if 5 shows up it chooses to spawn something other than 5, the same 3 prefabs keep showing up, this is really strange and I have no idea what could be causing this

This is on the player

function OnControllerColliderHit(hit: ControllerColliderHit){

if(hit.gameObject.CompareTag("Spawner")) {
		 randgen = Random.Range(1,8);
		 hitspawn = true;
		 Destroy(hit.gameObject);

And this is on the chunk of level:

#pragma strict
//Variables
var thePrefab1 : GameObject;
var thePrefab2 : GameObject;
var thePrefab3 : GameObject;
var thePrefab4 : GameObject;
var thePrefab5 : GameObject;
var thePrefab6 : GameObject;
var thePrefab7 : GameObject;
var thePrefab8 : GameObject;


//place tube units infront of current tube
function Update () {
	if (PlayerCollisions.hitspawn  PlayerCollisions.randgen == 1) {
		var instance0 : GameObject = Instantiate(thePrefab1, Vector3(transform.position.x, transform.position.y - 5.1f, transform.position.z + 35.5f), Quaternion.Euler(0, 0, 0));
		PlayerCollisions.hitspawn = false;
		Destroy (GetComponent (PrefabSpawnScript));
		}
	if (PlayerCollisions.hitspawn  PlayerCollisions.randgen == 2) {
		var instance1 : GameObject = Instantiate(thePrefab2, Vector3(transform.position.x, transform.position.y - 5.1f, transform.position.z + 35.5f), Quaternion.Euler(0, 0, 0));
		PlayerCollisions.hitspawn = false;
		Destroy (GetComponent (PrefabSpawnScript));
		}
//ect all the way up to 8

I realize my spawn script isn’t very pretty but it works for now.

Do I have an ordering problem or something?

add a debug.log to see what values you are getting.

Also when using int’s in a range the max value will never come unless the max = the min so you need to go to 9.

Thanks,it goes up to 8, but what seems to be happening is some numbers wont spawn the one they’re supposed to. For example 8 spawns prefab 4.

I also checked all the prefabs, all of them have 1-8 correctly assigned in the inspector.

there is definitely an error on your end somewhere… Just out of curiosity, why do you do it so complicated ( write so much repetitive code) instead of using a array?

because I’m incompetent, but I’ve ran through all the prefabs and I don’t see what could be happening…

lol ^^
I would recommend to rewrite your code, its a mess!
some recommendations from me

first of all create a array of Gameobjects

var prefabs : GameObject[];
  1. do the collision detection on the spawning script itself if possible (I dont see any reason why not)

on collision generate a random numder

var randomNumber : int = Random.Range(0, prefabs.Lenght);
  1. Instantiate the GameObject
var instance : GameObject = Instantiate(prefabs[randomNumber ], Vector3(transform.position.x, transform.position.y - 5.1f, transform.position.z + 35.5f), Quaternion.Euler(0, 0, 0));

this should work fine

thanks so much for the advice! Also I fixed my problem, it was really stupid, for prefabs 5 and above I copy pasted the names of the prefab variables so it just spawned 1 through 4 again. I feel really dumb now. If I just scripted more efficient code in the first place I would have never run into the problem.

Might also say that you need to add 1 higher number at the end. so if you like to have random numbers from 1 to 8 then you should use

var rndNumber : int = Random.Range(0,9)

so the best things is to use the length of an array as maximum value (as index starts at 0), as element_wsc already wrote.