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
0
Question by SirMacJefferson · Oct 03, 2011 at 02:26 AM · coroutineyieldinvoke

Invoke isn't working for a function with yield?

(JavaScript) Here are two sections of code in one script attached to a cube (the 'player'):

    public function ReleaseGun()
    {
         print("Started release");
         weaponry.WieldedWeapon.CurrentlyReleasing = true;
         yield WaitForSeconds(weaponry.WieldedWeapon.ReleaseTime);
         if (weaponry.WieldedWeapon.CurrentlyReleasing == true)
         {
             weaponry.WieldedWeapon.currentFiresLeft = weaponry.WieldedWeapon.ReleaseCapacity;
             weaponry.WieldedWeapon.CurrentlyReleasing = false;
             print("Release successful");
         }
    }

    if (Input.GetKeyDown("r") && weaponry.WieldedWeapon.CurrentlyReleasing == false)
    {
        Invoke("ReleaseGun",0);
    }
The if is in Update() (sorry for the sloppy code example, I don't want to try to get the code format to work with two separate pieces...it hates me). I tried Invoke with ReleaseGun just printing something. It worked and printed immediately after hitting R. I tried Invoke with everything but the "yield" in ReleaseGun. It worked, but instantly instead of WaitForSeconds working. When I add yield before WaitForSeconds, nothing happens at all. The first print isn't printed and no variables are set.

By the way, weaponry is a variable based off of a class called Weaponry. WieldedWeapon is also set to a class called Weapon, which has various weapon-related stats, including ReleaseTime, a float. The ReleaseTime of the specific weapon I was using is 3, and I printed that to verify it.

When I just regularly call the function via "ReleaseGun()", it works perfectly, but I want to invoke it so I can CancelInvoke in the middle if you change your weapon (this is like a reload system in an MOFPS).

Why does the 'yield' seem to destroy the function...?

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

2 Replies

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

Answer by aldonaletto · Oct 03, 2011 at 02:58 AM

Invoke is internally implemented as a coroutine, like ReleaseGun (yield transforms the whole function in a coroutine), but this shouldn't be a problem, since coroutines can start other coroutines.
Could it be possible to transfer part of the code to Update, removing the yield instruction from ReleaseGun?

    if (Input.GetKeyDown("r") && weaponry.WieldedWeapon.CurrentlyReleasing == false)
    {
        weaponry.WieldedWeapon.CurrentlyReleasing = true;
        Invoke("ReleaseGun",weaponry.WieldedWeapon.ReleaseTime);
    }
You still could cancel invoke while the release time wasn't ended.
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 Eric5h5 · Oct 03, 2011 at 05:13 AM 0
Share

I don't think Invoke is implemented as a coroutine.

avatar image SirMacJefferson · Oct 03, 2011 at 05:44 AM 0
Share

Thanks! I did what you said and it's working, and I made the weapon changing function cancel the invoke and that works fine, too.

Also, thanks for fixing up the code blocks up there...I can never seem to get them right.

avatar image aldonaletto · Oct 03, 2011 at 06:26 PM 0
Share

This format button sometimes refuses to work - I use the following tags:

(always let a blank line before the first tag)
<pre>
(code goes here)
</pre>

It has always worked fine for me in questions, answers and comments (much different from that damned format button...)

avatar image
0

Answer by Eric5h5 · Oct 03, 2011 at 05:12 AM

You can't use yield with Invoke. I'd recommend not using Update, and instead doing everything as a coroutine. Instead of WaitForSeconds, make a function that is similar to how WaitForSeconds works but can be interrupted.

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 aldonaletto · Oct 04, 2011 at 01:18 AM 0
Share

@Eric5h5, I read something about Invoke and InvokeRepeating being implemented as coroutines, but just can't remember when or where (not in the docs, I guess). If more than one Invoke/InvokeRepeating command may be active at the same time, it makes sense to use some kind of coroutine to implement these commands - you must allocate an independent control structure for each command instance, check it periodically, etc. $$anonymous$$aybe they don't use the same coroutines as us, simple mortals, but it must be a similar mechanism.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How does Invoke cope with mapping on frames 1 Answer

Waiting for input using yield and coroutines 1 Answer

StartCoroutine important for using yield? 1 Answer

How to use yield within a class function 2 Answers

JS Wait for a function to complete - no WaitForSeconds 1 Answer


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