• 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
21
Question by ibyte · Nov 22, 2009 at 04:20 PM · coroutineyield

How do I use Yield in C#?

How does one use yield in C#. It behaves differently then the Javascript version of yield.

I was trying to convert t$$anonymous$$s Javascript example to C#. It doesn't seem to call the corfunAlp routine. Help!

using UnityEngine; using System.Collections;

public class fader : MonoBehaviour {

 public GameObject gamobjBac;
 Color thecolor;

 // Use t$$anonymous$$s for initialization
 void Start ()
 {
     corfunAlp (gamobjBac, 0, 1, 1);
     // Turn opaque 
     corfunAlp (gamobjBac, 4, 1, 0);
     // Turn transparent     
 }

 // Update is called once per frame
 void Update ()
 {
 }

 //function that fades alpha for a game object up or down with a start delay and duration of fade
 public IEnumerator corfunAlp (GameObject gamobjGamObj, float floWai, float floDur, int intAlpGoa)
 {

     float floAlp = (1 - intAlpGoa);
     intAlpGoa = ((2 * intAlpGoa) - 1);

     yield return new WaitForSeconds (floWai);

     w$$anonymous$$le (floAlp >= 0 && floAlp <= 1) 
     {
         thecolor = gamobjGamObj.renderer.material.GetColor ("_Color");
         thecolor.a = floAlp; 
         gamobjGamObj.renderer.material.SetColor ("_Color", thecolor);

         floAlp = floAlp + ((Time.deltaTime/floDur)*intAlpGoa);    
         yield return true; 
     }
 }

}

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
17
Best Answer

Answer by Murcho · Nov 22, 2009 at 04:44 PM

You need to manually call StartCoroutine() for C# coroutines. That information is provided in the documentation here.

To use it in you example, simply change your Start function as follows.

// Use t$$anonymous$$s for initialization
void Start ()
{
        StartCoroutine(corfunAlp (gamobjBac, 0, 1, 1));
        // Turn opaque 
        StartCoroutine(corfunAlp (gamobjBac, 4, 1, 0));
        // Turn transparent     
}
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
12

Answer by equalsequals · Nov 22, 2009 at 07:53 PM

Murcho is correct. But for additional help I wrote t$$anonymous$$s tutorial aw$$anonymous$$le back on Coroutines for both JS and C#:

http://infiniteunity3d.com/2009/09/27/tutorial-coroutines-pt-1-waiting-for-input/

Hope that helps!

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 Michael La Voie · Nov 22, 2009 at 08:55 PM 0
Share

Thank you, that was very helpful! I'm sure you're busy, please add the screen shots back in if you ever have the time.

avatar image equalsequals · Nov 22, 2009 at 09:29 PM 0
Share

Haha - you have no idea. I was hoping to get to that some time this evening if I can!

avatar image equalsequals · Nov 23, 2009 at 03:13 PM 0
Share

Just an FYI I was able to get those images back into the blog post last night. Cheers!

avatar image Michael La Voie · Nov 26, 2009 at 08:46 AM 0
Share

I really appreciate it. This was just a case of perfect timing as I was just starting to puzzle over coroutines. Thanks again!

avatar image Michael La Voie · Nov 26, 2009 at 08:53 AM 1
Share

FYI, @equalsequals, not that you haven't done plenty already, but you may be interested in this tool by google called Google code Prettify. I've used it before and its quite easy, just add the JS and it will do code coloring for your example code: http://code.google.com/p/google-code-prettify/

Show more comments
avatar image
0

Answer by synapsemassage · Jul 16, 2011 at 11:11 AM

And there is even some more info in the docs. Here.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Pause the coroutine inside a loop 1 Answer

Last item not being moved (yield problem) 0 Answers

Coroutoutine doesnt work properly. 3 Answers

Getting an error when trying to yield 2 Answers

Im a bit confused on a simple script 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