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 Benifir · Jul 29, 2013 at 11:45 PM · js-c#

Yield WaitForSeconds Javascript to C# Problem

I can't get these few lines to work correctly in C#. Any help would be great, here is the segment.

 function UpdateRandom ()
 {
     while(true)
     {
         randomPush = Random.insideUnitSphere * randomForce;
         yield WaitForSeconds(randomFreq + Random.Range(-randomFreq / 2, randomFreq / 2));
     }    
 }

And yes, I know it cannot return a void in C#. Anyways, thanks for taking a look!

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 perchik · Jul 29, 2013 at 11:50 PM

Actually you can return void in C#, that's not the problem

Your problem is that in C# you cannot just yield from a normal function block. You have to yield out of an IEnumerator function and call it from StartCoRoutine

the short answer is this:

 public IEnumerator UpdateRandom()
 {
     while(true)
     {
         randomPush= Random.insideUnitSphere*randomForce;
         yield new WaitForSeconds(randomFreq + Random.Range(-randomFreq / 2, randomFreq / 2));
     }
 }


With that said though, I have no clue what you're doing here and I don't think you're using random correctly.

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 Benifir · Jul 29, 2013 at 11:55 PM 0
Share

I meant you cannot use a yield in a function returning void (: I am just converting a few C# scripts and this one controls the movement of birds. Anyways thanks for the help.

avatar image
0

Answer by Bunny83 · Jul 29, 2013 at 11:53 PM

 public IEnumerator UpdateRandom ()
 {
     while(true)
     {
         randomPush = Random.insideUnitSphere * randomForce;
         yield return new WaitForSeconds(randomFreq + Random.Range(-randomFreq / 2f, randomFreq / 2f));
     }    
 }
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 Stevenwithaph · Jul 29, 2013 at 11:59 PM

The syntax is wrong in a few places

Functions in C# begin with the return type and to use a coroutine the function must return a IEnumerator

The correct syntax would for the function declaration would be

 IEnumerator UpdateRandom()

And the correct syntax for the yield is

 yield return new WaitForSeconds(blahblah)

The scripting reference provides a quick way to check how to do things between the languages, there's an option in the top left to change what language you're viewing the reference in, so just flip between javascript and c# to see the difference.

Assuming randomPush is declared elsewhere, this should be all the changes

 IEnumerator UpdateRandom ()
 {
     while(true)
     {
        
        randomPush = Random.insideUnitSphere * randomForce;
        yield return new WaitForSeconds(randomFreq + Random.Range(-randomFreq / 2, randomFreq / 2));
     }  
 }


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.

Follow this Question

Answers Answers and Comments

19 People are following this question.

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

Related Questions

js to c# converting trouble 1 Answer

javascript > c# conversion help 1 Answer

Trying to convert Js to C# 1 Answer

Question when Converting JS to C# 2 Answers

how to reference a js script attached to a prefabs child from a c# script attached to another prefab? 0 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