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
2
Question by MagicFrame · Oct 24, 2013 at 05:18 PM · coroutinecoroutine errors

StartCoroutine_Auto can only be called from the main thread.

Hi I'm having some problems with my code, I can assure that worked well, but now I jump with coroutines error ... Here is the code to see if anyone can help me ... I am using Ngui, then when you press a button called one of the coroutines, but now I get the error.

 public IEnumerator WaitAlertView()
     {
         yield return new WaitForSeconds (0.2f);
         EtceteraBinding.hideActivityView();
         yield return new WaitForSeconds (0.6f);
         EtceteraBinding.showAlertWithTitleMessageAndButton("Error","Es posible que el nombre de usuario ya exista, por favor escoge otro nombre","Vale");
         
     }
     
     
     public IEnumerator WaitAlertView2()
     {
         
         yield return new WaitForSeconds (0.2f);
         EtceteraBinding.hideActivityView();
         yield return new WaitForSeconds (0.6f);
         EtceteraBinding.showAlertWithTitleMessageAndButton("Error","Se ha cancelado el registro","Vale");
     }
     
     public IEnumerator WaitAlertView3()
     {
         
         yield return new WaitForSeconds (0.2f);
         EtceteraBinding.hideActivityView();
         yield return new WaitForSeconds (0.6f);
         EtceteraBinding.showAlertWithTitleMessageAndButton("Error","El Login ha fallado, comprueba que su usuario y password son correctos","Vale");
     }
     
     public IEnumerator WaitAlertView4()
     {
         
         yield return new WaitForSeconds (0.2f);
         EtceteraBinding.hideActivityView();
         yield return new WaitForSeconds (0.6f);
         cargado = true;
     }
     
     
     
     // Update is called once per frame
     public void Update () 
     {
         
         
         myName = nameuser.text;
         myPassword = password.text;
         myEmail = email.text;
         loginUser2 = loginName.text;
         loginPass2 = loginPass.text;
         NivelCargado();
     
     
     
     }
     
     // Accion que se dispara cuando se pulsa el boton unirse a PLM
     public void SendUserToParse()
     {
         EtceteraBinding.showBezelActivityViewWithLabel("Creando usuario, un momento por favor");
         // Crear nuevo usuario en Base de datos
         
         var user = new ParseUser()
         {
             
         Username = myName,
         Password = myPassword,
         Email= myEmail
             
             
         };
         
         Task signUpTask = user.SignUpAsync().ContinueWith(t =>
         {
             if (t.IsCanceled)
             {
                 
                 StartCoroutine(WaitAlertView2());
             
                 
                 
             }
             else if ( t.IsFaulted)
             {
                 
                 
                 StartCoroutine(WaitAlertView());
                 
                 
             }
             else
             {
                 
                     StartCoroutine(WaitAlertView4());
             }
             
         });
         
         
     }
     
     // Accion que se dispara cuando se pulsa el boton Login
     public    void Login()
     {
         EtceteraBinding.showBezelActivityViewWithLabel("Conectando, un momento por favor");
         
         
         ParseUser.LogInAsync(loginUser2,loginPass2).ContinueWith(t=>
         {
             
             if (t.IsFaulted || t.IsCanceled)
             {
                 
                 StartCoroutine(WaitAlertView3());
                 
             }
             
             
             else 
             {
                 StartCoroutine(WaitAlertView4());
                 
             }
             
         
         
         });
         
         
     }
     
     
     // Cuando el login se ha creado con exito cambiamos de escena
     public void NivelCargado ()
     {
         
         if (cargado)
         {
             
             
             Application.LoadLevel ("EscenadeJuego");
         }
         
     }
 
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
12
Best Answer

Answer by Wolkenschauer · Feb 15, 2014 at 12:06 AM

well it sounds more like you're looking for a Invoke method with all the waiting to me, however if you want to start a coroutine from a different thread than the unity main thread, therefore you could create a list and add whatever the coroutine is supposed to do to it as for instance an action and invoke the action on a gameobject within the game loop (i use this kind of approach whenever i have network calls which are by nature asnchronious ...

1) create a gameobject in your scene and add a this script:

 public class DoOnMainThread : MonoBehaviour {
     
     public readonly static Queue<Action> ExecuteOnMainThread = new Queue<Action>();
     
     public virtual void Update()
     {
         // dispatch stuff on main thread
         while (ExecuteOnMainThread.Count > 0)
         {
             ExecuteOnMainThread.Dequeue().Invoke();
         }
     }
 }

2) Add your coroutine action into the queue whenever you want to call it like this:

 DoOnMainThread.ExecuteOnMainThread.Enqueue(() => {  StartCoroutine(WaitAlertView4()); } );

it will be executed next opportunity the main thread can, or rather when the game object will call it's update method

Comment
Add comment · Show 5 · 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 shaochun · Sep 28, 2014 at 02:46 PM 0
Share

After days of search this works perfectly for me. $$anonymous$$any thanks!

avatar image SuneK · Feb 12, 2015 at 06:06 PM 0
Share

This needs to be the accepted answer. Very nice - especially for asynchronious execution such as networking as you mention!

avatar image Issah · Jun 25, 2015 at 10:00 AM 0
Share

Very good answer ! $$anonymous$$aybe it's obvious but in order to use Queue class you have to include:

 using System.Collections.Generic;

avatar image bk0606 · Jan 11, 2016 at 01:15 PM 1
Share

Thank you very much, so helpful! But maybe there need queue data lock before ExecuteOn$$anonymous$$ainThread.Dequeue().Invoke();?

avatar image xibanya · Dec 12, 2019 at 04:35 AM 0
Share

thanks, that was perfect!

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

20 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 avatar image

Related Questions

Coroutine Local Scale Stops Milliseconds After Starting 1 Answer

Coroutine Errors 0 Answers

spliting my code in a coroutine with yield return null? 2 Answers

Enemy attack constantly with a coroutine 1 Answer

Coroutine Scale goes past maxSize, but still keeps scaling. 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