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 /
avatar image
0
Question by martipello · Aug 25, 2017 at 03:02 PM · if-statementsboolmethodcheckasync

check if method is finished, bools not working

Im using bools to check if methods are finished before doing something with a list but for some reason its not working Im using Firebase to get some info from my database it retrieves the values in async tasks (Im aware they could finish in any order) sets a bool to true to indicate its finished and then calls a method and creates a custom object from those values like this

        universe7.GetValueAsync().ContinueWith(task => {
         if (task.IsFaulted)
         {
             // Handle the error...
         }
         else if (task.IsCompleted)
         {
             u7done = false;
             DataSnapshot snapshot = task.Result;
             int i = 10;
             foreach (var child in snapshot.Children)
             {
                 if (child.Child("erased").GetValue(true).ToString() == "1"){
                     //if this doesnt work put it in a if bracket and do uo.killCount--
                     i--;
                 }
             }
             u7done = true;
             if (i > 0)
             {
                 killCount(new uniObj(
                     7, i, "Universe", "Beerus", "Whis", "Shin", "Gohan", "Kill Count : " + totalKillsList[6], u7Badge
                 ));
             }
             i = 0;
         }
     });


this works perfectly. The method it calls - killCount(new uniObj(...)) - takes an uniObj adds it to a list and then if the bool u7done is true it does some more stuff, but there is actually more than one bool to check there is u2done, u3done, u4done, u6done, u7done, u9done, u10done and u11done and they are all set to true in identical methods to the one above with slightly differing values so for instance the u6 version of the above method looks like this

         universe6.GetValueAsync().ContinueWith(task => {
         if (task.IsFaulted)
         {
             // Handle the error...
         }
         else if (task.IsCompleted)
         {
             DataSnapshot snapshot = task.Result;
             u6done = false;
             int i = 10;
             foreach (var child in snapshot.Children)
             {
                 if (child.Child("erased").GetValue(true).ToString() == "1")
                 {
                     //if this doesnt work put it in a if bracket and do uo.killCount--
                     i--;
                 }
             }
             u6done = true;
             if (i > 0)
             {
                 killCount(new uniObj(
                     6, i, "Universe", "Champa", "Vados", "Fuwa", "Cabba", "Kill Count : " + totalKillsList[5], 
                     u6Badge
                 ));
             } 
             
             i = 0;
         }
     });


my kill count method checks to see if all of the bools are true before executing the code but for some reason it isn't working. The method is called, the object is added to the list but then stops despite all the bools being true (they are public so i can see them in the inspector) am i not setting them to true early enough? i use a similar system (near on identical) in another script that works flawlessly, is there something else going on that i dont know about or a better way of check that all the methods have completed? can anybody help me please? here is my killCount method it checks all the bools in one statement

 public void killCount(uniObj obj)
 {
     print(obj.Universe + " count is " + finalUniverseList.Count);
     finalUniverseList.Add(obj);

     if (u2done && u3done && u4done && u6done && u7done && u9done && u10done && u11done)
     {

         finalUniverseList.Sort((x, y) => x.FightersLeft.CompareTo(y.FightersLeft));
         for (int i = 0; i < mostKillsFighters.Count; i++)
         {
             GameObject goc = mostKillsFighters[i];
             goc.GetComponent<Image>().sprite = finalUniverseList[i].Image;
             foreach (Transform tr in goc.transform)
             {
                 foreach (Transform tr2 in tr)
                 {
                     if (tr2.gameObject.tag == "kill_count")
                     {
                         tr2.GetComponent<Text>().text = finalUniverseList[i].FightersLeft.ToString();
                     }
                     if (tr2.gameObject.tag == "name")
                     {
                         tr2.GetComponent<Text>().text = finalUniverseList[i].Name + " " + finalUniverseList[i].Universe.ToString();
                     }
                     if (tr2.gameObject.tag == "info")
                     {
                         tr2.GetComponent<Text>().text =
                             "God: " + finalUniverseList[i].GodName + "\r\n" +
                             "Angel: " + finalUniverseList[i].AngelName + "\r\n" +
                             "Kai: " + finalUniverseList[i].KaiName + "\r\n" +
                             "Captain: " + finalUniverseList[i].capName + "\r\n" +
                             finalUniverseList[i].Kills;
                     }
                 }
             }

             print(finalUniverseList.Count + " count " + i);
         }
         print("list was cleared");
     }
 }
Comment
Add comment · Show 2
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 FortisVenaliter · Aug 25, 2017 at 03:06 PM 1
Share

https://en.wikipedia.org/wiki/Race_condition

avatar image martipello FortisVenaliter · Aug 25, 2017 at 03:09 PM 0
Share

thanks, but what does it all mean basil? lol surely the way ive tried to solve it should work then no?

1 Reply

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

Answer by FortisVenaliter · Aug 25, 2017 at 03:17 PM

Well, take your last example. Your code basically says "if all these bools are true at this very moment, do this logic. Otherwise, don't do it and return"

Race conditions are typically very difficult to debug, especially so when just looking at code snippets.

What you probably want to do instead is "while these bools are false, wait. Once they are true, do this logic". But then you need to look out for infinite loops if any dependent process fails.

Comment
Add comment · Show 2 · 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 martipello · Aug 25, 2017 at 03:50 PM 0
Share

genius! thank you

avatar image martipello · Aug 25, 2017 at 04:12 PM 0
Share

added a while loop in my update function that checks for these bools and moved the code from my kill count method into it so my kill count method now looks like this

public void killCount(uniObj obj) { finalUniverseList.Add(obj); } lol the rest is in a while loop in the update function

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

68 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

Related Questions

Entering false statement even it is at true state. 1 Answer

How to detect certain text in an input field? 2 Answers

Conversion method types(void to bool)[SOLVED] 1 Answer

script on monster checks if bool on attacking player = true? 2 Answers

SetActive not working 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