Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Joe 22 · Apr 13, 2011 at 10:46 PM · yieldwaitforseconds

yield WaitForSeconds() causing compiler errors

I have two classes:

class SubScreen extends MonoBehaviour
{
    function Open()
    {
        SetShow( true );
        RandomUtils.ToggleVisibility( gameObject );
    }
}

and

class SubScreen_AdvGameOver extends SubScreen { override function Open() { super();

     yield WaitForSeconds(1);
             // looking to do some more stuff down here...
 }   

}

However, this causes a compiler error: BCE0101: Return type 'void' cannot be used on a generator. Did you mean 'IEnumerator'? You can also use 'System.Collections.IEnumerable' or 'object'.

I have used yield WaitForSeconds() in numerous other places and it has worked fine. I've looked up to see if anyone else has had problems, and I found things like calling the function from Update() and OnGUI() -- The Open() function is not called from either of those. I can declare other functions in the child class that are allowed to have a yield in them, and the compiler does not throw any errors.

Under what circumstances can you yield in a function?

Comment
Add comment · Show 1
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 Joe 22 · Apr 13, 2011 at 10:54 PM 0
Share

It looks like the super() call in the override function may be to blame for the compiler error -- it's possible to call the yield in the parent function, but only if there is no override function...but I do not understand what/why the issue is.

1 Reply

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

Answer by Joe 22 · Apr 13, 2011 at 11:07 PM

I think I may have found the answer, but it would be great if someone a little more technical could explain what is going on...

The following code fixes this and works:

class SubScreen extends MonoBehaviour
{
    function Open() : IEnumerator
    {
        SetShow( true );
        RandomUtils.ToggleVisibility( gameObject );
    }
}

and

class SubScreen_AdvGameOver extends SubScreen { override function Open() : IEnumerator { yield super.Open();

     yield WaitForSeconds(1);
             // looking to do some more stuff down here...
 }   

}

By the nature of the compiler error, I suspected that the functions had to return IEnumerator (even though other functions I have used yield in return void). However, the tricky part was then figuring out to yield super.Open(). Trying yield super() does not work... I'm not clear about why the syntax is the way it is, or why the functions had to return IEnumerator despite other functions using yield and not having to.

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 Zaqnafein · Oct 30, 2013 at 11:25 PM 0
Share

Hi Joe, I think I have a similar problem: link text. I have added the return type EInumerator to my functions but then the coroutine is never executed...

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

1 Person is following this question.

avatar image

Related Questions

Mysteries of yield 1 Answer

A delay/yield/wait function for function Update? 6 Answers

Create a 10 second delay using WaitForSeconds 2 Answers

Why does this not work? 1 Answer

WaitForSeconds not running 2 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