I creat the 30 empty gameobject in different places.
I have cube and sphere.
Start game, I want to randomly the 10 of empty gameobject were in place show cube or sphere.
(Cube and Sphere location are empty gameobject)
I know “random.range” this script.
But this can only set a range, and can’t specify the location of objects.
What can i write the code? thanks!
this is just one method of many that you can use:
make your cube and sphere into prefabs .
create a script file call it something like globalCount.js or anything you like.
inside this script write a global variable to count the number of generated objects like this :
static var objectCount:int=0;
create another script call it generateObjects.js or something you like .
in it add this code :
var cubePrefab:gameObject;//set these in the inspector
var spherePrefab:gameObject;//set these in the inspector
var haveAnObject:boolean=false;
function Update()
{
if(globalCount.objectCount<10 !haveAnObject)
{
var rand=Random.Range(0,100);//to see if we should create an object
if(rand>50)
{
haveAnObject=true;
globalCount.objectCount +=1;
rand=Random.Range(0,100);//to see if we should make a cube or sphere
if(rand>50)
Instantiate (cubePrefab, transofrm.position, transform.rotation);
else
Instantiate (spherePrefab, transofrm.position, transform.rotation);
}
}
}
attach generateObjects.js to all your empty gameobjects
You can use arrays…
Here will be script:
var spawnPoints : Transform[]; // Array of spawn points to be used.
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var amountEnemies = 20; // Total number of enemies to spawn.
var yieldTimeMin = 2; // Minimum amount of time before spawning enemies randomly.
var yieldTimeMax = 5; // Don't exceed this amount of time between spawning enemies randomly.
function Start(){
Spawn();
}
function Spawn(){
for (i=0; i<amountEnemies; i++){ // How many enemies to instantiate total
yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax)); // How long to wait before another enemy is instantiated.
var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next.
Instantiate(obj, pos.position, pos.rotation);
}
}
Thanks shaderx and badbii.
First I use shaderx’s method and I succeed.
my prefabs has cube and sphere, I want to create capsule and cylinder.
There are 4 objects in my prefabs.
How can i edit generateObjects.js?
except add…
var capsulePrefab:gameObject
var cylinderPrefab:gameObject
thanks a lot.
if you use badii script it would work automaticaly , in the inspector adjust the array enemyPrefabs size to 4 and attach your prefabs to each slot , of course you’ll have to set the positions too .
if you use my script you’d have to adjust the code a little .
change this :
if(rand>50)
Instantiate (cubePrefab, transofrm.position, transform.rotation);
else
Instantiate (spherePrefab, transofrm.position, transform.rotation);
to this:
if(rand>75)
Instantiate (cubePrefab, transofrm.position, transform.rotation);
else if(rand>50)
Instantiate (spherePrefab, transofrm.position, transform.rotation);
else if(rand>25)
Instantiate (capsulePrefab, transofrm.position, transform.rotation);
else
Instantiate (cylinderPrefab, transofrm.position, transform.rotation);
I edited before you answer
As below:
if(rand<=20)
Instantiate (coinA, transform.position, transform.rotation);
if(rand<=40 rand>20)
Instantiate (coinB, transform.position, transform.rotation);
if(rand<=60 rand>40)
Instantiate (coinC, transform.position, transform.rotation);
if(rand<=80 rand>60)
Instantiate (coinD, transform.position, transform.rotation);
if(rand<=100 rand>80)
Instantiate (coinPass, transform.position, transform.rotation);
It seems possible!?
About badbii script
I don’t understand yieldTimeMin and yieldTimeMax
My game has no use for time~
After start game, error is: Array index is out of range