Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by HugoLeblanc · Oct 03, 2017 at 11:02 PM · coroutines

Getting a value outside of Coroutine

Hello, I'm actually developping a game on Unity, and I'm stuck on a problem for few days now. I think I'm doing something totally wrong but after searching again and again I found no way. I want to load my characters which are in a MySQL database. I'm using Coroutines to get the values from my PHP scripts, but it looks like I'm getting the coroutine AFTER my game loads characters, but i'm calling the coroutine before it. I'm getting the values, but I can't get them outside of my coroutine in my LoadCharacter. If i'm outputing the values IN my coroutine, I will see the value (CharacterName) that I want, but if I output the CharacterName in my LoadCharacter, it wont.

I'm sorry if I didn't explain well, but I hope that someone will help me just a little bit ! Thank you.

     public CharacterSaveData[] LoadAllCharacters()
     {
         StartCoroutine(GetCharacters());
         string[] charactersList = ZombieHunters.gameManager.CharactersList.Split ("," [0]);
         List<CharacterSaveData> chars = new List<CharacterSaveData> ();
 
         if (charactersList.Length > 0)
         {
             for (int i = 0; i < charactersList.Length; i++)
             {
                 if (charactersList[i] != "")
                 {
                     CharacterSaveData ch = LoadCharacter(charactersList[i]);
                     if (ch.PlayerName != "")
                     {
                         chars.Add(ch);
                     }                    
                 }
             }
 
             CharacterSaveData[] allCharacters = chars.ToArray ();
 
             return allCharacters;
         }
         return null;
     }
 
     public CharacterSaveData LoadCharacter(string charID)
     {
         CharacterSaveData character = new CharacterSaveData ();
         StartCoroutine(LoadChar(charID));
 
         character.PlayerName = ZombieHunters.gameManager.CharacterName;
         character.CharacterIndex = ZombieHunters.gameManager.CharacterIndex;
         Debug.Log("Character Name : " + ZombieHunters.gameManager.CharacterName + ". Character Index : " + ZombieHunters.gameManager.CharacterIndex);
         character.CharacterKey = charID;
         return character;
     }
 
     IEnumerator GetCharacters()
     {
         string uri = "http://localhost/keep-alive/api/getchars.php"; //
         string accountID = ZombieHunters.gameManager.AccountID;
 
         WWWForm form = new WWWForm();
         form.AddField("accountID", accountID);
 
         WWW www = new WWW (uri,form);
 
         yield return www;
         ZombieHunters.gameManager.CharactersList = www.text;
     }
 
     IEnumerator LoadChar(string _characterID)
     {
         string uri = "http://localhost/keep-alive/api/loadchar.php"; 
         int characterID = Int32.Parse(_characterID);
         
         WWWForm form = new WWWForm();
         form.AddField("characterID", characterID);
 
         WWW www = new WWW (uri,form);
 
         yield return www;
         string[] values = www.text.Split(new char[] {'\n'});
 
         ZombieHunters.gameManager.CharacterName = values[0];
         ZombieHunters.gameManager.CharacterIndex = Int32.Parse(values[1]);
         Debug.Log("Load Char Character Name : " + ZombieHunters.gameManager.CharacterName + ". Character Index : " + ZombieHunters.gameManager.CharacterIndex); 
     }
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

2 Replies

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

Answer by HugoLeblanc · Oct 05, 2017 at 03:26 PM

I fixed this by using the new C# feature async-await which is new to Unity 2017. http://www.stevevermeulen.com/index.php/2017/09/23/using-async-await-in-unity3d-2017/ Here is a good web page that explains it.

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 Cuttlas-U · Oct 04, 2017 at 04:38 AM

hi; thats a big script :D and u did very good coding;

Im not pro at scripting but the thing i thought maybe i can help y with and worth testing is that when ever u run a corutine u dont wait for it to finish ; u continue your function but there is a small delay betwerrn runing corutines and wait for it to recive data;

so check if u wait for it things will be changed or not ;

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 HugoLeblanc · Oct 04, 2017 at 01:20 PM 0
Share

Well thank you, I kinda knew it and did wait for it to finish yesterday, but I guess that I did wrong because it made my app wait forever. I simply created a bool and put it true when my coroutine started, false when it ended. Do you have any idea how I could wait for it by a clean way ? Anyway now I know 100% what is the problem and I thank you for that.

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

116 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 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 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 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 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 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 Error when Capturing 360 Video 0 Answers

Why Unity docs do not use New with WaitForSeconds and yield? 1 Answer

What is Image.Start() ?? and How can i reduce this function's GC.Collect 0 Answers

Heavy use of Unity API & UI Thread 0 Answers

If a variable used by a coroutine is changed, will the coroutine know while it is running? 1 Answer


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