• 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
3
Question by Kichang-Kim · Nov 17, 2010 at 09:24 AM · yieldconvert

How do I use yield in a C# RPC function?

Hi. I tried to convert javascript to C# with Networking sample.

My code almost works, but some code doesn't work in NetworkLevelLoad.js (RPC function).

 //NetworkLevelLoad.js
 
 @RPC
 function LoadLevel (level : String, levelPrefix : int)
 {
     // omitted code
 
     Application.LoadLevel(level);
     yield;
     yield;
 
     // Allow receiving data again
     Network.isMessageQueueRunning = true;
     // Now the level has been loaded and we can start sending out data
     Network.SetSendingEnabled(0, true);
 
     // Notify our objects that the level and the network is ready
     for (var go in FindObjectsOfType(GameObject))
         go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver); 
 }

I convert yield; to yield return 0;, and edited LoadLevel function to IEnumerator LoadLevel. (Of course I converted other lines to C# too.)

But at runtime, after first yield return 0, script is ended and Network.IsMessageQueueRunning is not performed. How can I edit this script for work fine?

Thanks.

Comment
Add comment · Show 3
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 denewbie · Nov 17, 2010 at 10:24 AM 0
Share

Are you getting any errors in your debugger?

avatar image Kichang-Kim · Dec 01, 2010 at 08:37 AM 0
Share

no error. but $$anonymous$$essageQueining after yield is not excuted.

avatar image Herman-Tulleken · Jul 16, 2011 at 11:15 AM 0
Share

I also want to know how to make yield work in RPC calls. It looks like they are not called if the return type of the RPC function is not void.

3 Replies

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

Answer by Azzara. · Nov 05, 2011 at 01:31 PM

I'm currently struggling with the same task of converting the networking example to C#

The problem is that yield containing functions must return an Enumerator but RPC functions must return void.

The following code works for me.

 [RPC]
 public void LoadLevel(string level, int levelPrefix)
 {
   StartCoroutine(loadLevel(level, levelPrefix));
 }
 
 private IEnumerator loadLevel(string level, int levelPrefix)
 {
   // omitted code
 
   Application.LoadLevel(level);
   yield return new WaitForEndOfFrame();
   yield return new WaitForEndOfFrame();
 
   // Allow receiving data again
   Network.isMessageQueueRunning = true;
   // Now the level has been loaded and we can start sending out data
   Network.SetSendingEnabled(0, true);
 
   // Notify our objects that the level and the network is ready
   foreach (GameObject go in FindObjectsOfType(typeof(GameObject)))
     go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
 }
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
-1

Answer by denewbie · Nov 17, 2010 at 10:28 AM

Meantime, I dont have my debugger with me now but I'll convert it to something like this. Does it look similar to yours?

public void LoadLevel (string level, int levelPrefix) { // omitted code

 Application.LoadLevel(level);
 yield;
 yield;

 // Allow receiving data again
 Network.isMessageQueueRunning = true;
 // Now the level has been loaded and we can start sending out data
 Network.SetSendingEnabled(0, true);

 // Notify our objects that the level and the network is ready
 foreach (GameObject go in FindObjectsOfType(GameObject))
     go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver); 

}

Comment
Add comment · Show 3 · 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 Kichang-Kim · Nov 19, 2010 at 07:51 AM 0
Share

thanks for reply. but "yield;" occured error in C#.

avatar image Peter G · Feb 12, 2011 at 02:21 PM 0
Share

You cannot do coroutines like that in C#. You must return IEnumerator and the yield syntax is as follows "yield return 'value';" And, never "yield return 0;". There is an unnecessary memory allocation. Return null ins$$anonymous$$d.

avatar image Kichang-Kim · Jul 05, 2012 at 04:38 AM 0
Share

Thanks Peter G

avatar image
0

Answer by kajjait · May 15, 2012 at 07:51 PM

this is one major mind fuck thanks to c# being nearly useless, and javascript being well, javascript.

so i'm assuming we're all working off of the same code, and in order to fix the problem you need to make the following changes:

 [RPC]
 public void LoadLevel(string level, int levelPrefix)
 {
   StartCoroutine(loadLevel(level, levelPrefix));
 }
     
 private IEnumerator loadLevel (string level, int levelPrefix)
 {
         
     showLobby = false;
     Debug.Log("Loading level " + level + " with prefix " + levelPrefix);
     lastLevelPrefix = levelPrefix;
 
     // There is no reason to send any more data over the network on the default channel,
     // because we are about to load the level, thus all those objects will get deleted anyway
     Network.SetSendingEnabled(0, false);    
 
     // We need to stop receiving because first the level must be loaded.
     // Once the level is loaded, RPC's and other state update attached to objects in the level are allowed to fire
     Network.isMessageQueueRunning = false;
         
     // All network views loaded from a level will get a prefix into their NetworkViewID.
     // This will prevent old updates from clients leaking into a newly created scene.
     Network.SetLevelPrefix(levelPrefix);
     Application.LoadLevelAdditive(level);
     yield return new WaitForEndOfFrame();
     yield return new WaitForEndOfFrame();
     Debug.Log("Loading complete");
         
       
 
     Debug.Log("load level DONE");
     // Allow receiving data again
     Network.isMessageQueueRunning = true;
     // Now the level has been loaded and we can start sending out data
     Network.SetSendingEnabled(0, true);
         
     Debug.Log("sending load msg");
     // Notify our objects that the level and the network is ready
     foreach (GameObject go  in FindObjectsOfType(typeof(GameObject)) )
     {
             Debug.Log("sending load msg");
             go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);    
     }
 }


so level additive works and i dont know exactly why, but you need to change your function to a non RPC with return type IEnumerator so that yield will work properly. and in order to call as a RPC, make a dummy function with return type void. and start a coroutine so yield can work its magic.

Comment
Add comment · Show 4 · 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 Bunny83 · May 15, 2012 at 08:18 PM 1
Share

What's the point in answering a 2 years old question with the same answer that is already there (Azzara) ?

Btw you posted the answer two times. I deleted your second one since you had all your text bold and it's the same again ...

Why do you complain about C#? Generator functions always work this way, even before Unity came out.

I guess you just didn't used DontDestroyOnLoad on this gameobject, right? So if you load a new level this gameobject + script + coroutine is wiped out.

avatar image kajjait · May 26, 2012 at 02:23 AM 0
Share

i dunno why it posted twice or in bold, but thanks for your answer. i probably didnt do that! just trying to adjust to this magical world of managed code. i'm used to having full control of how my program runs. my bad!?

avatar image Kichang-Kim · Jul 05, 2012 at 04:33 AM 0
Share

DontDestroyOnLoad ! I don't know that method until now! Very thanks.

avatar image Kichang-Kim · Jul 05, 2012 at 04:37 AM 0
Share

And thanks for reply kajjait

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Getting cosinus of an angle in degrees 3 Answers

Can i convert int to long? 1 Answer

how to convert gps coordinates to unity 2 Answers

Is there a way to convert a euler vector from 0 to 360 --> -90 to 90? 0 Answers

How to properly use Yield for a coroutine? 3 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