- Home /
Adding a timer to spawner
So for class im making a project that uses one button only and not allowed the default rigidbody physics. I made all the movement and distance restrictions for the character my self but i need fish to spawn and cant seem to make it work. This is a script i found on the site and it just makes them all spawn everywhere, within the range limitation. How can i add a timer to this so they spawn periodically.
var thePrefab : GameObject; var range: float = 12; // set the +/- range around the empty object
function Update () { var pos = transform.position; // get this object's position pos.y += Random.Range(-range, range); // add a random x offset around it var instance : GameObject = Instantiate(thePrefab, pos, transform.rotation); }
Answer by fafase · Sep 26, 2012 at 08:14 AM
var thePrefab : GameObject; var range: float = 12;
function Start(){
InvokeRepeating("Spawn",0.01f,5.0f);
}
function Spawn () {
var pos = transform.position;
pos.y += Random.Range(-range, range);
var instance : GameObject = Instantiate(thePrefab, pos, transform.rotation);
}
With InvokeRepeating() the first parameter is the function you want to use, the second is when do you want the first call, I put 0.01f because 0.0f is buggy and call the function twice. Then the third parameter is the frequency of call. Every 5 seconds.
Answer by ImPvEspec · Sep 26, 2012 at 10:23 AM
Invoke repeating... of course :) thanks so much works like a charm
Your answer
Follow this Question
Related Questions
How can I rotate a shooting-practice target 180 degrees with a timer in C#? 1 Answer
Random/Procedural Object spawner? 1 Answer
Trying to switch Textures on object based on timer 2 Answers
Need help with randomize attack time. Please help! 2 Answers
Spawning an object at a random time | C# 2 Answers