- Home /
Question by
FH_88 · Jan 11, 2015 at 04:08 AM ·
javascriptgameobjectrandomspawnamount
Spawn random amount of gameobjects
I have a script which spawns a gameobject every fourth second, but my problem is that it only spawns one object, I would like to spawn a random amount between two numbers for example 1 and 5. How would I do this?
My Script.
var Mines : Transform;
private var Timer : float;
function Awake () {
Timer = Time.time + 4;
}
function Update () {
if (Timer < Time.time) {
Instantiate(Mines, transform.position, transform.rotation);
Timer = Time.time + 4;
Debug.Log("Spawned Mine");
}
}
Comment
Answer by getyour411 · Jan 11, 2015 at 04:18 AM
Random.Range (1,6)
you probably want to do something with transform.pos or they'll all spawn at the same place
Thank you for the great information, because right now the problem is that the amount of objects, spawn in the same place and I dont really get how I would fix this.
So far my script looks like this.
var $$anonymous$$ines : Transform;
private var Timer : float;
function Awake () {
Timer = Time.time + 4;
}
function Update () {
var randNum = Random.Range(0, 10); // this will return a number between 0 and 9
var screenPosition : Vector3 = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0,Screen.width), Random.Range(0,Screen.height), Camera.main.farClipPlane/2));
if (Timer < Time.time) {
for (var i = 0; i < randNum; i++){
Instantiate($$anonymous$$ines, screenPosition, Quaternion.identity);
//Instantiate($$anonymous$$ines, transform.position, transform.rotation);
Timer = Time.time + 4;
Debug.Log("Spawned $$anonymous$$ines");
}
}
}
Answer by knuckles209cp · Jan 11, 2015 at 04:19 AM
function Update() {
var randNum = Random.Range(0, 10); // this will return a number between 0 and 9
for (var i = 0; i < randNum; i++){
Instantiate(Mines, transform.position, Quaternion.identity);
//other stuff } }