Bumbaz
1
Hi guys. I'm kind of new to this. I want cubes to spawn randomly in the game every, let's say, 5 seconds. I have this code so far:
var object:Transform;
var x = 12;
var z = 12;
var y = 5;
function Update () {
var position = Vector3(Random.Range(-x, x), y, Random.Range(-z, z));
Instantiate(object, position, Quaternion.identity);
}
I tried using yield WaitForSeconds(5.0) but it messed things up... Thank you in advance.
~Bumbaz
AliAzin
2
you can't use yield in Update(). you should do it in start(), Awake() or custom functions like this:
function Start(){
DoInstantiate();
}
function DoInstantiate(){
while (true){
yield WaitForSeconds(5.0);
var position = Vector3(Random.Range(-x, x), y, Random.Range(-z, z));
Instantiate(object, position, Quaternion.identity);
}
}
illinar
3
C# code I would recommend:
public GameObject spawnObject;
public float xRange;
public float zRange;
public float y;
public float spawnRate;
void Awake ()
{
InvokeRepeating("Spawn", 0f, spawnRate);
}
void Spawn ()
{
Instantiate(spawnObject, new Vector3(Random.Range(-xRange, xRange), y, Random.Range(-zRange, zRange)), Quaternion.identity);
}
ok in java script for unity this is how you get the script to spawn a item and when it gets destroyed to let it spawn agein.
var bullet : GameObject;
var bulletbackup : GameObject;
function Update () { if( Input.GetButton(“fire1”))
{bulletbackup = Instantiate(bullet, transform, rotation,))}
}
Now to make a spawn time on a object is like this
var spawntime : float = 1;
var resetnumber : float = 10;
function Update (){Time.deltaTime * ++spawntime;
}
That might help but the best way Ive made a spawn time is by useing the animation tool
But I hope this helps.
Oh and im not for sure on the script with the spawn time but that is done in Java