- Home /
Question by
MrStebone · Oct 05, 2013 at 04:51 PM ·
spawning-enemies
radom spawn points
hi there, im working on a game which has a maze im wondering how i would create random spawn points for my prefab?
pragma strict
var enemy01 : GameObject;
function Start () {
Instantiate (enemy01,this.transform.position, Quaternion.identity);
}
function Update () {
}
this currently lets me spawn one prefab from my empty game object, i would like to use an array to create these random spawn points of five prefabs.
thanks
Comment
Best Answer
Answer by AjayKhara · Oct 05, 2013 at 05:35 PM
Try this,
#pragma strict
import System.Collections.Generic;
var enemyPrefab : GameObject;
var positions : Transform[];
private var listPositions : List.<Transform> = new List.<Transform>();
function Start () {
for(pos in positions){
listPositions.Add(pos);
}
while(listPositions.Count > 0){
var tempPos : Transform = listPositions[Random.Range(0,listPositions.Count)];
GameObject.Instantiate(enemyPrefab, tempPos.position, tempPos.rotation);
listPositions.Remove(tempPos);
}
System.GC.Collect();
}
Create random spawn points in the scene, where you want your enemy to be spawned at, and attach those spawn points to the positions array.
Your answer
![](https://koobas.hobune.stream/wayback/20220613115356im_/https://answers.unity.com/themes/thub/images/avi.jpg)