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
0
Question by MobinYaqoob · Nov 12, 2015 at 08:29 AM · unity 5unity 2dwwwinternet

Execution Order of Statement is not corrent

Okay first of all give you a brief introduction of the function i created: its a function which checks wheather the net is working or not if net is working it fetch the image and set image.sprite Or if it is not working then gives a debug.log("Not working the net"); So Here is the function :

 public void LoadImage()
         {
             StartCoroutine(CheckInternet());
             //Call the actual loading method
             if (isInternet == false)
             {
                 StartCoroutine(RealLoadImage());
     
             }
             else
             {
                 Debug.Log("No InternetConnection is Enable");
             }
         }

On Button Click Event this function is executed. Here is the function which checks the internet

  IEnumerator CheckInternet()
         {
             WWW internet = new WWW("www.google.com");
             yield return internet;
             if (internet.error != null)
             {
                 isInternet = false;
             }
             else
             {
                 isInternet = true;
             }
             yield return null;
         }

It check the light page forexample : google.com if it returns the google.com it means net is working but for my scenerio in function CheckInternet() WWW internet = new WWW("www.google.com"); yield return internet; it exits from here yield statement but it should countinue to next statement to make isInternet false or true. Thanks in advance :)

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
1

Answer by HenryStrattonFW · Nov 12, 2015 at 10:38 AM

The problem here is that you are not waiting for your internet connection check to finish before acting on the expected results.

StartCoroutine is used to start a process that may take 1 frame, or even several seconds to complete. Coroutines are often used for WWW requests because they very much asynchronous no matter how quick they can sometimes be.

In order to wait for your internet connection check to finish, you either need to run your LoadImage method as a coroutine as well, yielding on the StartCoroutine of the CheckInternet. OR pass a callback method into your check internet function, once the check is complete, it can then call the callback to act on the results. for example.

 public void LoadImage()
 {
     StartCoroutine( CheckInternet( ( bool lHasInternet ) =>
     {
         if( lHasInternet )
         {
             Debug.Log( "We have a connection." );
             // Trigger your image load.
         }
         else
         {
             Debug.Log( "No internet." );
         }
     } ) );
 }
 
 
 IEnumerator CheckInternet( System.Action<bool> lCallback )
 {
     WWW internet = new WWW( "www.google.com" );
 
     yield return internet;
 
     if( lCallback != null )
     {
         lCallback( string.IsNullOrEmpty( internet.error ) );
     }
 }

NOTE: However, is there any particular reason you are checking your connection this way instead of using something like a check on the value of Application.internetReachability ?

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

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

42 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

Related Questions

Asset Bundle not compatible between different unity versions 1 Answer

Overlap ScrollRect 1 Answer

Instantiating A group of platform and moving them down 0 Answers

CommandInvokationFailure: Unable to merge android manifests. ,Unable to merge android manifest Error 0 Answers

Unity 2D rotation not smooth? 0 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