- Home /
How do I put a delay in this script ?
I have a fairly simple script that spawns weapons at a choice of locations. Currently it spawns a new weapon the exact moment the old one is collected. How do I go about putting a delay on that?
I thought a simple 'yield WaitForSeconds()' statement would do it, but you having no luck. I've tried adding the waitforseconds before/after almost every line in the script but it either has no effect at all, the weapon spawn goes wild or I get error messages about functions can't be coroutines?
#pragma strict
var spawnPoint : Transform[]; // Create list of possible spawn locations
var wepType : GameObject[]; // Creat list of possible weapons to be spawned
var wepPresent : boolean = false;
function Start()
{
SpawnWeapons(); //Run SpawnWeapons coroutine
}
function Update()
{
if(wepPresent == false) //Check to see if weapon is present
{
SpawnWeapons(); //If no weapon is present run SpawnWeapons coroutine
}
}
function SpawnWeapons() //Spawns a random weapon at one of possible spawn locations
{
var randomLoc : int = Random.Range(0,spawnPoint.length); //Pick random number between the min and max values of the array containing the spawn locations.
var randomWep : int = Random.Range(0,wepType.length); //Pick random number between the min and max of the array containing the weapon types.
var newWeapon = Instantiate(wepType[randomWep], spawnPoint[randomLoc].position, spawnPoint[randomLoc].rotation);
newWeapon.GetComponent(DestroySelfOnContact).getSpawnHandler = this.gameObject;
//Pick random weapon depending on which number was generated by 'randomWep'
//Spawn weapon at location depending on which number was generated by 'randomLoc'
wepPresent = true;
if(newWeapon.GetComponent(DestroySelfOnContact).destroyed == true)
{
wepPresent = false;
}
}
Any suggestions please guys ???
Sorry, what exactly does that mean/do ?
And also that gives me the following error -
Assets/Scripts/RandomWeaponRandomLocation.js(15,35): BCE0005: $$anonymous$$ identifier: 'frame'.
???
private var frame : int;
var DelayInFrames : int = 2500;
function Update() {
frame++;
if (!wepPresent && frame % DelayInFrames == 0) { //This will check if remainder of frame divided by DelayInFrames is 0 (If the frame number is 2500 dividable by DelayInframes and it is a round number)
SpawnWeapons();
}
}
Answer by GameVortex · Jan 09, 2015 at 02:30 PM
You can supply the delay to the SpawnWeapons function and start a WaitForSeconds coroutine: Example:
function SpawnWeapons(delay : float)
{
wepPresent = true; //Immediately change so the SpawnWeapons will not be constantly called by Update
yield WaitForSeconds(delay);
//Do weapon spawning here
}
getting errors -
Assets/Scripts/RandomWeaponRandomLocation.js(21,23): BCE0043: Unexpected token: var.
Assets/Scripts/RandomWeaponRandomLocation.js(21,27): BCE0044: expecting EOF, found 'delay'.
???
Sorry about that, I don't usually work in UnityScript. But as a recommendation: ins$$anonymous$$d of being stopped by every single little compile error you get you should search around and try to figure out what the issue could be and learn from it. Especially when it is about something as basic as parameters for a function.
The Scripting tutorials by unity is good place to start: =) http://unity3d.com/learn/tutorials/modules/beginner/scripting/variables-and-functions
Anyway, I edited my answer to be more correct.
Turns out
function SpawnWeapons()
{
wepPresent = true; //Immediately change so the SpawnWeapons will not be constantly called by Update
yield WaitForSeconds(delay);
}
is actually what I needed. Didn't like having anything in the function brackets.
The change I made to your line, plus declaring my var at start of script and now works fine.
Wouldn't have thought of it though without your help, so many thanks !
:)
Your answer
Follow this Question
Related Questions
Better way for a timer to load a level? 1 Answer
C# simple delay execution without coroutine? 2 Answers
Speed does not work? 1 Answer
Help With Initiating Bomb Every Specific Second 1 Answer
Increasing spawn rate over time 1 Answer