- Home /
Question by
debu · Jul 20, 2013 at 12:27 PM ·
randomrandomspawningautomate
Auto Creation of Objects
I have a Space Scene, as follows:
I have a prefab for asteroid objects; my query is how can I create a kind of asteroid belt between Mars and Jupiter.
Please guide.
image 2.png
(52.3 kB)
Comment
Answer by Professor Snake · Jul 20, 2013 at 12:32 PM
You can use Instantiate() to create new objects. The following example code will create 10 asteroids randomly placed around a planet.
var asteroidPrefab:GameObject;
var minDistance:float=10;
var maxDistance:float=20;
function PlaceAsteroids(){
for(var i:int=0;i<10;i++){
var offset:Vector2=Random.insideUnitCircle*Random.Range(minDistance,maxDistance);
Instantiate(asteroidPrefab,transform.position+Vector3(offset.x,0,offset.y),Random.rotation);
}
}
Thanks or the reply, is it somehow possible to see the created asteroid on the Scene Editor..?? So that I can better see and modify certain other variables.
thanks in advance :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613110507im_/https://answers.unity.com/themes/thub/images/avi.jpg)