hi to all …
how can i Instantiate 2 or more GameObject to random ?
example i have 3 Prefab .
var Bullet1 : GameObject ;
var bullet2 : GameObject ;
var bellet3 : GameObject ;
Instantiate(Bullet1 ,Vector3(-26,0,16.5), Quaternion.identity);
hi to all …
how can i Instantiate 2 or more GameObject to random ?
example i have 3 Prefab .
var Bullet1 : GameObject ;
var bullet2 : GameObject ;
var bellet3 : GameObject ;
Instantiate(Bullet1 ,Vector3(-26,0,16.5), Quaternion.identity);
Sorry, could you elaborate on what you want to randomize? You can use Random.Range to pick random numbers, like this:
var min = 0.0;
var max = 10.0;
function Update () {
//Every frame choose a new random number between 0 and 10
Random.Range (min, max);
}
You can do all sorts of randomized behaviors by assigning results to numbers and picking a random number.
Here is how I do it:
Top of code:
GameObject clone;
a function later on that I call in my invaders game
void setupBunkers()
{
for (int i=0;i<4;i++)
{
clone=(GameObject) Instantiate(objBunker);
modScale=clone.transform.lossyScale;
pos[0]=70+((i*50)-modScale[0]-40);
pos[1]=(-70);
pos[2]=0;
clone.transform.position=pos;
}
}
Hope this helps.
I need to randomize the Instantiate of 3 objects, namely 3 types of bullets …
random.range it random only number not objects …
thanks for all Helps
how about something simple like this:
randomNumber = Random.Range (0, 3);
switch(randomNumber)
{
case 0: Instantiate(Bullet1 ,Vector3(-26,0,16.5), Quaternion.identity); break;
case 1: Instantiate(Bullet2 ,Vector3(-26,0,16.5), Quaternion.identity); break;
case 2: Instantiate(Bullet3 ,Vector3(-26,0,16.5), Quaternion.identity); break;
}
or you can always use array randomize the index if you want to be fancy
thank you very much …
the code is perfect …
Ideally, you’d want to decouple the instantiation and the random selection.
If you stored the GameObjects in an array then you could just do
var randomBullet : GameObject = bullet[Random.Range(0,3)];
Instantiate(randomBullet, ... );
I’d do basically the same thing as Peter in concept, but, I’d write it maybe slightly differently. The function is exactly the same, but just a matter of personal preference. Here’s the full code (untested, but, I think it should work)
// Set these in the inspector
var bullets : GameObject[];
function GenerateBullet() {
// Get a random bullet index
var bulletNum : int = Random.Range(0, bullets.length);
// Generate the bullet
Instantiate(bullets[bulletNum], Vector3(-26,0,16.5), Quaternion.identity);
}
Thanks guys, in the end I used all 2 for 2 different roles,
This is a beautiful community, thanks