Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by TheEmeralDreamer · Aug 31, 2011 at 09:22 PM · timedeltatime

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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...
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheEmeralDreamer · Aug 31, 2011 at 10:27 PM 0
Share

When I try this I keep getting an error that says :

the referenced script on this behavior is missing

avatar image DaveA · Sep 01, 2011 at 12:27 AM 0
Share

This means there is no script on your object. $$anonymous$$ake sure there is, and it includes this code.

avatar image Magius96 · May 22, 2015 at 04:47 PM 0
Share

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.

avatar image
8

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheEmeralDreamer · Aug 31, 2011 at 10:34 PM 0
Share

I've tried everything short of copy and pasting Dave's code. Doesn't seem to work for me still.

avatar image
3

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 :)

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PatHightree · Aug 31, 2011 at 09:37 PM 0
Share

Coroutines FTW !

avatar image TheEmeralDreamer · Aug 31, 2011 at 09:46 PM 0
Share

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.

avatar image Ipwndthebeast · Jan 29, 2014 at 01:55 PM 0
Share

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?

avatar image OtsegoDoom · Jan 29, 2014 at 02:45 PM 0
Share

@TheEmeralDreamer

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.

avatar image Ipwndthebeast · Jan 29, 2014 at 02:48 PM 0
Share

I want to attack as much as i want i just dont want the enemy instantly gone.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Different behavior between editor and standalone changing time step 0 Answers

Time.timeScale and Time.fixedDeltaTime 1 Answer

Time.delta Time inconsistant 5 Answers

Make something change color for a set amount of time 3 Answers

Time.deltaTime messing with object speed increase 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges