# Random.Range won't return max value?

I have an enemy spawn script that is supposed to be spawning the enemies in one of four random locations. So it spawns fine in locations 1-3, but for some reason never gets to 4. Here is my code I’m not sure if it will help.

``````var spawning = false;
var spawnPoint : int;
var theLocation : Transform; Instantiate (theLocation , theLocation.position, transform.rotation);
var level : int;
function Start () {
spawning = false;
level = 1;
cooldown();
}

function Update () {

if(spawnPoint == 1 && spawning){
Instantiate(theLocation,Vector3 (0,-8,0), Quaternion.identity);
spawning = false;
cooldown();
};
if(spawnPoint == 2 && spawning){
Instantiate(theLocation,Vector3 (0,8,0), Quaternion.identity);
cooldown();
spawning = false;
};
if(spawnPoint == 3 && spawning){
Instantiate(theLocation,Vector3 (-8,0,0), Quaternion.identity);
cooldown();
spawning = false;
};
if(spawnPoint == 4 && spawning){
Instantiate(theLocation,Vector3 (8,0,0), Quaternion.identity);
cooldown();
spawning = false;
};
}
function cooldown () {

for (var x = 1; x < 2; x++) {

if (level == 1){
yield WaitForSeconds(2);
spawning = true;
spawnPoint = Random.Range(1,4);
}
if(level == 2){
yield WaitForSeconds(1);
}
spawnPoint = Random.Range(1,4);
spawning = true;
}
}
``````

Hi,

I’ve not used this function yet - but when using the INT overload for Random.Range - the docs say this:

Description
Returns a random integer number between min [inclusive] and max [exclusive] (Read Only).
If max equals min, min will be returned. The returned value will never be max unless min equals max.

In short, the MAX number won’t get returned, it’s exclusive, hence you’ll get values from 1 to 3 only.

So, I think the answer is this:

``````spawnPoint = Random.Range(1,5);
``````

?? Give that a try

The upper limit is exclusive. It wont reach it in the integer version of random range.

try

`````` Random.Range(1,5);
``````

http://docs.unity3d.com/ScriptReference/Random.Range.html