# Instantiate mass prefab, random.range the same???

Im making some levels for my game where i Instantiate alot of enemys to shoot down, when i Instantiate the prefabs witch i have put on a ‘‘move’’ script. where i use some Random.Range. The random nomber seems to be the same on all prefabs and they all get the same behaivor. - how can i get around this?

Instantiate script:

``````var enemyONE:Transform;
var enemyTWO:Transform;
private var nextEnemy = 0.0;
private var tid = 0.0;

function Update() {

tid = tid+0.1;

if(tid <= 300  tid >= nextEnemy){

var helloEnemy = Instantiate(enemyTWO, transform.position+Vector3(Random.Range(-3, 7),0,0), transform.rotation);

nextEnemy =	tid + Random.Range(20, 70);

}
}
``````

move scipt on the Instantiated object :

``````private var leftRight = 0;
private var upDown = -1;
var joe = 0;

function Update () {

var posY = transform.position.y;

if(posY <= 6){

joe = joe + Random.Range(0,1);

if(joe == 1){

leftRight = 1;
upDown = 0;

}
if(joe == 0){

leftRight = -1;
upDown = 0;

}

}

transform.Translate(leftRight*Time.deltaTime,upDown*Time.deltaTime,0);

}
``````

Here in the move script my goal is to get the object to move one way or the other on a random faktor i hope you see… if 1 then one way and if 0 then the other way… but they all move the same way!? - i hope you understand my problem, im new give me a hint! :0)

That’s quite a few posts you have there!!

dunno why, please help me… then you can have some of my post :0) and a free version of my free for everyone game who’s got the king kong wow wiiMote!

This happens if you put random transform noise on two objects too, they move in sync unless you change some aspect

Your insantiating from 1 empty Game Object ,right?

make another: eg:

``````var newObject: GameObject;

function Start() {
while (true) {
yield WaitForSeconds(30);
Instantiate(newObject, transform.position, transform.rotation);

}
}
``````

On one empty and

``````var newObject: GameObject;

function Start() {
while (true) {
yield WaitForSeconds(40);
Instantiate(newObject, transform.position, transform.rotation);
//Loop so this repeats every 20 secs?
}
}
``````

on another? Mod these to suit your needs.
AC[/code]

yeah, but how did he get a gazillion posts?

(helped me too, targos, thanks…)

1337 h4x. Its got to be the l3e+ |-|aX.

Must be a bug, 256256256.

Suspiciously, in tracking down a solution to this OTHER problem, I see the same number of users in the OpenGL profiler in reference to the PACE extensions in Unity…

NisEik is haxxoring my system!!

From the docs:

Usage
int Range (int min, int max)

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.

Note that this means that this Random.Range call always returns 0:
joe = joe + Random.Range(0,1);

Either change the parameters to floats or use Random.Range(0,2).

Btw, are you sure you want to increment the joe (joe = joe + SomeNumber) from your move script it seems like you want to reasign it instead (joe = SomeNumber)

BTW, there’s an error in the docs for Random.Range.

``````// Loads a random level from the level list
``````

Unless Joe has added more undocumented features, I very much doubt this will load anything :twisted:.

Just use
Random.Range(0.0,1.0);
Random.Range(0,1);

hello and very thx to you guys! - i found my way around it… so here is what i came out with for my enemy move if any wanna see… witch is Instantiated from only one emty game object and the random works just fine…

``````private var leftRight = 0;
private var upDown = -1;
private var move1 = 0;
private var move2 = 0;
private var tid = 0.0;

function Update () {

var posY = transform.position.y;
tid = tid+0.5*Time.deltaTime;

if(tid>0.0  tid < 0.1){
move1 = Random.Range(3.0,6.0);
move2 = Random.Range(0.0,3.0);
var plus = 0;
var kant1 = 0;
var kant2 = 0;
var startTid =0;
var startTid2 = 0;
}

if(move1 >= 4.0){
if(move1 <= posY  posY <= move1+0.5){
leftRight = -1.5;
upDown = 0;
startTid = 1;
}
}

if(move1 < 4.0){
if(move1 <= posY  posY <= move1+0.5){
leftRight = 1.5;
upDown = 0;
startTid = 1;
}
}

if(beslut >= 4.0){
if(move2 <= posY  posY <= move2+0.5){
leftRight = 1.5;
upDown = 0;
startTid2 = 1;
}
}

if(beslut < 4.0){
if(move2 <= posY  posY <= move2+0.5){
leftRight = -1.5;
upDown = 0;
startTid2 = 1;
}
}

// pr?ver at sikre at de g?r ned f?r de rammer kanten.
if(startTid == 1  tid > 2.5){
leftRight = 0;
upDown = -1.5;
}
// sikre at de g?r ned f?r de rammer kanten i minus siden.
if(posX == -3.4  startTid == 1){
leftRight = 0;
upDown = -1.5;
kant1 = 1;
}
// sikre at de g?r ned f?r de rammer kanten i pluds siden.
if(posX == 6.6  startTid == 1){
leftRight = 0;
upDown = -1.5;
kant2 = 1;
}

// sikre at de g?r ned f?r de rammer kanten i minus siden.
if(posX == -3.4  startTid2 == 1){
leftRight = 0;
upDown = -1.5;
kant1 = 1;
}
// sikre at de g?r ned f?r de rammer kanten i pluds siden.
if(posX == 6.6  startTid2 == 1){
leftRight = 0;
upDown = -1.5;
kant2 = 1;
}

var pos = transform.position + Vector3 (leftRight*Time.deltaTime, upDown*Time.deltaTime, 0);
pos.x = Mathf.Clamp (pos.x, -3.4, 6.6);
pos.y = Mathf.Clamp (pos.y, -8.0, 6.0);
transform.position = pos;

}
``````

please make a comment if this is not the way to get this result… :0)(sorry! not ment in a bad way, im a very polite person!)

Where I come from, “gay” denotes is a sexual orientation (which no doubt some people in this community share) and not an adjective describing a type of hack.

Just hoping to keep the discussion polite.

d.