Author Topic: Bug or wrong code?  (Read 4587 times)

Magic Frame

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Bug or wrong code?
« on: October 24, 2013, 11:27:14 AM »
Hi I have worked with this code for two weeks without problems, since you update to 3.0.2 I am having a problem, namely that


"StartCoroutine_Auto can only be called from the main thread.
Constructors and field initializers will be Executed from the loading thread When loading a scene.
Do not use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function. "

any idea how to fix this?

  1. using UnityEngine;
  2. using System.Collections;
  3. using Parse;
  4. using System.Threading.Tasks;
  5. using System;
  6. using System.Collections.Generic;
  7.  
  8.  
  9. public class MenuLogin : MonoBehaviour
  10.  
  11. {
  12.         public GameObject myPanelLogin;         // Panel login
  13.         public UILabel nameuser;                        // llamamos al script donde se escribre el nombre de usuario
  14.         public UILabel password;                        // llamamos al script donde se escribre el Password
  15.         public UILabel email;                           // llamamos al script donde se escribre el mail
  16.         public UILabel errores;                         // llamamos al script donde se escriben los errores
  17.         public string myName;                           // copiamos el nombre de usuario en esta variable
  18.         public string myPassword;                       // copiamos el password en esta variable       
  19.         public string myEmail;                          // copiamos el email en esta variable
  20.         private bool cargado = false;           // registro completo pasar a la pantalla de juego
  21.         public UILabel loginName;
  22.         public UILabel loginPass;
  23.         public string loginUser2;
  24.         public string loginPass2;
  25.         public int controlActivities;
  26.        
  27.        
  28.        
  29.        
  30.        
  31.        
  32.         // Update is called once per frame
  33.         public void Update ()
  34.         {
  35.                
  36.                
  37.                 myName = nameuser.text;
  38.                 myPassword = password.text;
  39.                 myEmail = email.text;
  40.                 loginUser2 = loginName.text;
  41.                 loginPass2 = loginPass.text;
  42.                 NivelCargado();
  43.        
  44.        
  45.        
  46.         }
  47.        
  48.         // Accion que se dispara cuando se pulsa el boton unirse a PLM
  49.         public void SendUserToParse()
  50.         {
  51.                 EtceteraBinding.showBezelActivityViewWithLabel("Creando usuario, un momento por favor");
  52.                 // Crear nuevo usuario en Base de datos
  53.                
  54.                 var user = new ParseUser()
  55.                 {
  56.                        
  57.                 Username = myName,
  58.                 Password = myPassword,
  59.                 Email= myEmail
  60.                        
  61.                        
  62.                 };
  63.                
  64.                 Task signUpTask = user.SignUpAsync().ContinueWith(t =>
  65.                 {
  66.                         if (t.IsCanceled)
  67.                         {
  68.                                
  69.                                 StartCoroutine(WaitAlertView2());
  70.                        
  71.                                
  72.                                
  73.                         }
  74.                         else if ( t.IsFaulted)
  75.                         {
  76.                                
  77.                                
  78.                                 StartCoroutine(WaitAlertView());
  79.                                
  80.                                
  81.                         }
  82.                         else
  83.                         {
  84.                                
  85.                                         StartCoroutine(WaitAlertView4());
  86.                         }
  87.                        
  88.                 });
  89.                
  90.                
  91.         }
  92.        
  93.         // Accion que se dispara cuando se pulsa el boton Login
  94.         public  void Login()
  95.         {
  96.                 EtceteraBinding.showBezelActivityViewWithLabel("Conectando, un momento por favor");
  97.                
  98.                
  99.                 ParseUser.LogInAsync(loginUser2,loginPass2).ContinueWith(t=>
  100.                 {
  101.                        
  102.                         if (t.IsFaulted || t.IsCanceled)
  103.                         {
  104.                                
  105.                                 StartCoroutine(WaitAlertView3());
  106.                                
  107.                         }
  108.                        
  109.                        
  110.                         else
  111.                         {
  112.                                 StartCoroutine(WaitAlertView4());
  113.                                
  114.                         }
  115.                        
  116.                
  117.                
  118.                 });
  119.                
  120.                
  121.         }
  122.        
  123.        
  124.         // Cuando el login se ha creado con exito cambiamos de escena
  125.         public void NivelCargado ()
  126.         {
  127.                
  128.                 if (cargado)
  129.                 {
  130.                        
  131.                        
  132.                         Application.LoadLevel ("EscenadeJuego");
  133.                 }
  134.                
  135.         }
  136.  
  137.        
  138.         // Accion que se dispara cuando se pulsa unirse mediante Facebook
  139.        
  140.         public  void FacebookLogin()
  141.                
  142.         {
  143.                
  144.                
  145.                 //Task<ParseUser> loginTask = ParseFacebookUtils.LogInAsync( userId, accessToken, tokenExpiration);
  146.                
  147.         }
  148.        
  149.  
  150.  
  151. public IEnumerator WaitAlertView()
  152.         {
  153.                 yield return new WaitForSeconds (0.2f);
  154.                 EtceteraBinding.hideActivityView();
  155.                 yield return new WaitForSeconds (0.6f);
  156.                 EtceteraBinding.showAlertWithTitleMessageAndButton("Error","Es posible que el nombre de usuario ya exista, por favor escoge otro nombre","Vale");
  157.                
  158.         }
  159.        
  160.        
  161.         public IEnumerator WaitAlertView2()
  162.         {
  163.                
  164.                 yield return new WaitForSeconds (0.2f);
  165.                 EtceteraBinding.hideActivityView();
  166.                 yield return new WaitForSeconds (0.6f);
  167.                 EtceteraBinding.showAlertWithTitleMessageAndButton("Error","Se ha cancelado el registro","Vale");
  168.         }
  169.        
  170.         public IEnumerator WaitAlertView3()
  171.         {
  172.                
  173.                 yield return new WaitForSeconds (0.2f);
  174.                 EtceteraBinding.hideActivityView();
  175.                 yield return new WaitForSeconds (0.6f);
  176.                 EtceteraBinding.showAlertWithTitleMessageAndButton("Error","El Login ha fallado, comprueba que su usuario y password son correctos","Vale");
  177.         }
  178.        
  179.         public IEnumerator WaitAlertView4()
  180.         {
  181.                
  182.                 yield return new WaitForSeconds (0.2f);
  183.                 EtceteraBinding.hideActivityView();
  184.                 yield return new WaitForSeconds (0.6f);
  185.                 cargado = true;
  186.         }
  187.  
  188. }
  189.  


any helps? worked fine before, now even crash my iPhone

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Bug or wrong code?
« Reply #1 on: October 25, 2013, 02:43:12 AM »
It tells you in the error. You're likely calling your login from a constructor somewhere. Try moving it to an Awake() instead.

Magic Frame

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: Bug or wrong code?
« Reply #2 on: October 25, 2013, 02:55:23 AM »
Sorry, but do not really understand that I have to move to a Awake, all the functions that I have in this class, are initiated when you press a button, could be more explicit with your answer? Thanks for your help

Magic Frame

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: Bug or wrong code?
« Reply #3 on: October 28, 2013, 04:38:19 AM »
any help please?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Bug or wrong code?
« Reply #4 on: October 29, 2013, 01:31:54 AM »
This is not an NGUI question. The error comes from you trying to start a coroutine from a different thread. It has nothing to do with NGUI, which is why no one here is able to offer you much help.