• 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
2
Question by Lanipoop · Oct 15, 2010 at 06:26 AM · javascriptyield

How do I convert this Javascript function containing yield to C#?

I'm trying to convert a High Score tutorial from Javascript to C#. I've managed to convert all of the functions successfully, save for this one. So far I've gone from this:

function EnterHighScore (score : float) { // Setup the highscore table text SetupHighscoreTableText (); // Insert the entry, it might get rejected if the score is not high enough var entryIndex = InsertEntry (score); if (entryIndex == -1) return; // Check for the last name the user entered and reuse it var inputName = PlayerPrefs.GetString ("LastHighscoreName"); while (true) { for (var c : char in Input.inputString) { // Backspace - Remove the last character if (c == "\b"[0]) { if (inputName.Length != 0) inputName = inputName.Substring(0, inputName.Length - 1); } // End of entry. else if (c == "\n"[0]) { // But the user must have at least entered something if (inputName.Length) { ChangeName (entryIndex, inputName); SaveEntries (); // Store the name the user entered as the last high score name, // so next time the user doesnt have to enter it again PlayerPrefs.SetString ("LastHighscoreName", inputName); return; } } // Normal text - just append else { inputName += c; } } // Make sure the name doesnt grow above max entry length if (inputName.Length > maxNameLength) inputName = inputName.Substring (0, maxNameLength);

     // Add a "." as a blinking text marker.
     // Show the "." every .5 seconds
     blinkingName = inputName;
     var time = Mathf.Repeat (Time.time, 1.0);
     if (time > .5)
     blinkingName = inputName + ".";
     else
     blinkingName = inputName;
     // Change the name
     ChangeName (entryIndex, blinkingName);
     yield;
 }

}

to this:

void EnterHighScore (float score) { // Setup the highscore table text SetupHighscoreTableText(); // Insert the entry, it might get rejected if the score is not high enough var entryIndex = InsertEntry (score);

     if (entryIndex == -1)
     return;

     // Check for the last name the user entered and reuse it
     string inputName = PlayerPrefs.GetString("LastHighscoreName");
     while (true) 
         {
             foreach (char c in Input.inputString) 
             {
                 // Backspace - Remove the last character
                 if (c == "\b"[0]) 
                 {
                     if (inputName.Length != 0)
                     inputName = inputName.Substring(0, inputName.Length - 1);
                 }
                 // End of entry.
                 else if (c == "\n"[0]) 
                 {
                     // But the user must have at least entered something
                     if (inputName.Length)
                     {
                         ChangeName (entryIndex, inputName);
                         SaveEntries ();
                         // Store the name the user entered as the last high score name,
                         // so next time the user doesnt have to enter it again
                         PlayerPrefs.SetString ("LastHighscoreName", inputName);
                         return;
                     }
                 }
                 // Normal text - just append
                 else 
                 {
                     inputName += c;
                 }
             }

             // Make sure the name doesnt grow above max entry length
             if (inputName.Length > maxNameLength)
             inputName = inputName.Substring (0, maxNameLength);

             // Add a "." as a blinking text marker.
             // Show the "." every .5 seconds
             blinkingName = inputName;
             int time = Mathf.Repeat (Time.time, 1.0);

             if (time > .5)
             blinkingName = inputName + ".";
             else
             blinkingName = inputName;

             // Change the name
             ChangeName(entryIndex, blinkingName);
             yield;
         }
 }

At the line where it says "yield;", I get this error: "Only assignment, call, increment, decrement, and new object expressions can be used as a statement." What am I supposed to change it to?

And how do I convert this line to C#:

var entryIndex = InsertEntry (score);

Also, if you notice any more C# errors in the latter code, I'd appreciate any corrections!! Very much so!!!! ;)

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

1 Reply

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

Answer by duck · Oct 15, 2010 at 06:49 AM

Any function containing yield in Unity is a coroutine, which takes a little more conversion in C#.

First the function declaration needs to be modified so that its return type is IEnumerator, so:

function EnterHighScore (score : float) {

becomes:

public IEnumerator EnterHighScore (float score) {

Next, most yield statements generally needs a "return new" keyword added, eg:

yield WaitForSeconds(5);

becomes:

yield return new WaitForSeconds(5);

And a simple "yield" on its own becomes:

yield return null;

And finally, to call a coroutine in C#, you need to use the special function StartCoroutine(), rather than just call it as a regular function, so:

EnterHighScore(score);

becomes:

StartCoroutine(EnterHighScore(score));

For more information, see:

  • The syntax differences between C# and Javascript
  • Coroutines in the Manual
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 Lanipoop · Oct 15, 2010 at 07:16 AM 0
Share

Got it, thank you :)

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

No one has followed this question yet.

Related Questions

Need to call yield TWICE ??? (ANSWERED) 3 Answers

Simplifying javascript even further... 1 Answer

Yield WaitForSeconds doesn't work 1 Answer

Problem with yield WaitForSeconds (I think) -javascript) 1 Answer

Changing object location every 3 second 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