• 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
1
Question by Geovaughn · Apr 01, 2011 at 03:23 PM · yieldconverting

Yield not working in C#

I am trying to convert the Third Person Player Animation from JavaScript to C# and Unity is giving me an error that reads "error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement"

while(!playerController.IsGrounded())
    {
        yield;  
    }

Can anyone help convert this to C#?

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

4 Replies

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

Answer by Statement · Apr 01, 2011 at 03:49 PM

Can anyone help convert this to C#?

while(!playerController.IsGrounded())
{
    yield return null;  
}
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 loopyllama · Apr 01, 2011 at 04:37 PM 0
Share

also note to truly convert this, the function this is contained needs to return IEnumerator, meaning if this code is in function $$anonymous$$yFunc() becomes IEnumerator $$anonymous$$yFunc()

avatar image
0

Answer by e-bonneville · Apr 01, 2011 at 03:28 PM

In C#, you cannot call a yield in a standard function -- you have to use an IEnumerator. Here's a helpful link to the docs for you. Next to the script examples, on the upper left, switch the drop-down from the standard JS to C# to see the syntax differences. Here's an implementation of yield with IEnumerators for you as well:

using UnityEngine; using System.Collections;

public class example : MonoBehaviour { IEnumerator WaitAndPrint() { yield return new WaitForSeconds(5); print("WaitAndPrint " + Time.time); } IEnumerator Awake() { print("Starting " + Time.time); yield return WaitAndPrint(); print("Done " + Time.time); } }

Comment
Add comment · Show 7 · 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 Geovaughn · Apr 01, 2011 at 03:34 PM 0
Share

What is the equivalent of just the yield is? Because I still do not understand how to convert it.

avatar image e-bonneville · Apr 01, 2011 at 03:41 PM 0
Share

Sorry for not being clear. There is none. In JS, yield suspends a function; in C#, yield suspends an IEnumerator (another name for that is coroutine). There is no yield for C# in a function.

avatar image Statement · Apr 01, 2011 at 03:45 PM 0
Share

Isn't that yield return StartCoroutine(WaitAndPrint()); ?

avatar image Statement · Apr 01, 2011 at 03:48 PM 0
Share

yield exist in C# as well but you have to define what to yield. yield return something; or yield break;. "yield;" in C# would become "yield return null;" yield is used for enumerators (think iterators for C++ but a bit different) where code is executed until the next yield and the value is returned or the enumeration is breaked. It's the mechanism that drives foreach loops for instance.

avatar image e-bonneville · Apr 01, 2011 at 03:50 PM 0
Share

Really? Wow, well, I guess you learn something every day. Hehe, thanks for clearing that up, Statement. :-)

Show more comments
avatar image
0

Answer by GSP · Apr 21, 2011 at 07:25 AM

In c#, you must use the StartCoroutine(method()); to use the yield in your coding. Some how there is no alternative for this but you can try in your own code for waitforseconds() method. but it is to be confused and a bit bittrayed.

Comment
Add comment · 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
0

Answer by gbstack · Apr 28, 2014 at 04:07 AM

The official Unity documentation has something wrong with "yield" usage (it's using "yield return"), the correct syntax should be "yield return null". You can check this post for more information Unity Expression expected after yield return

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Why is yield not working for me? 2 Answers

How to toggle gameObject collision 2 Answers

Figuring out Coroutines 3 Answers

Why is my "yield" statement stopping my script? 1 Answer

how would i cause the function to wait until it stops to function again? 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