- Home /
Delay A Function Via Time
I have a function that I would like to limit the use of via two variables
var nextUsage;
var delay=2;//two seconds delay.
basically what I would like to do is set nextUsage upon start so that it has the value of the time. When the user uses the function, a check will be run to see if the time has passed, if so, use the function and add the delay amount to nextUsage.
I keep getting these two errors however:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) Movement.Update () (at Assets/Scripts/Movement.js:269)
which point to this line:
if(Input.GetButton("Spin")) { if(NextAttackTime>=Time.deltaTime)//line 269 { NextAttackTime+=AttackDelay;//set the nextattack time spinattack(); }
}
I'm a little confused. Any ideas?
Answer by DaveA · Aug 31, 2011 at 09:29 PM
Untested
var nextUsage;
var delay=2;//two seconds delay.
function Start()
{
nextUsage = Time.time + delay;
}
function Update()
{
if (Time.time > nextUsage)
{
nextUsage = Time.time + delay;
.. do stuff...
When I try this I keep getting an error that says :
the referenced script on this behavior is missing
This means there is no script on your object. $$anonymous$$ake sure there is, and it includes this code.
This was the answer I was looking for. I know about Coroutines, but for my usage a coroutine just wouldn't cut it since i needed to restart the timer whenever the object was touched.
Answer by testure · Aug 31, 2011 at 10:10 PM
for something like this I actually just use Invoke.
when the player hits the attack key and I want to wait half a second for the animation to sync up for example:
Invoke("DoAttack", .5f); // this will call the function DoAttack in .5 seconds.
edit: or are you talking about something like a cooldown? if so, then DaveA's example works fine for that.
I've tried everything short of copy and pasting Dave's code. Doesn't seem to work for me still.
Answer by Illogical-Ironic · Aug 31, 2011 at 09:32 PM
Perhaps this could help: http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html
Example:
yield WaitForSeconds(delay);
I hope that helps you somehow :)
I know of co routines but basically rather than waiting x amount of time, I would like to prevent an action from being taken until X time elapses.
Would this work if i have a grenade thrown at an enemy and i want to delay him being destroyed.
If not can you help me?
You can use yield WaitForSeconds together with a boolean to prevent actions from taking place.
Ex.
var attackReady : boolean;
function Attack () {
if (attackReady) {
//Enter your attack functionality here
attackReady = false;
}
yield WaitForSeconds(delay);
attackReady = true;
}
This will allow you to attack, and then you won't be able to attack again until the delay timer is over.
I want to attack as much as i want i just dont want the enemy instantly gone.
Your answer