Search Unity

Dispose WWW if timeout occurs in Unity3d

Discussion in 'Scripting' started by AshwaniKumar, Aug 21, 2013.

  1. AshwaniKumar

    AshwaniKumar

    Joined:
    Mar 19, 2013
    Posts:
    4
    I am trying to dispose a WWW object if a timeout occurs. I am using following code:

    Code (csharp):
    1. WWW localWWW;
    2.  
    3. void Start ()
    4. {
    5.     stattTime = Time.time;
    6.  
    7.     nextChange = Time.time + rotationSpeed;
    8.  
    9.     StartCoroutine ("DownloadFile");
    10.  
    11. }
    12.  
    13. bool isStopped = false;
    14. bool isDownloadStarted = false;
    15. // Update is called once per frame
    16. void Update ()
    17. {   //2.0f as to simulate timeout
    18.     if (Time.time > stattTime + 2.0f  !isStopped) {
    19.         isStopped = true;
    20.         isDownloadStarted = false;
    21.         Debug.Log ("Downloading stopped");
    22.         StopCoroutine ("DownloadFile");
    23.         localWWW.Dispose ();
    24.  
    25.     }
    26.     if (isDownloadStarted) {
    27.  
    28.     }
    29.  
    30.     if (Time.time > nextChange  isDownloadStarted) {
    31.         Debug.Log ("Current Progress: " + localWWW.progress);
    32.         nextChange = Time.time + rotationSpeed;
    33.     }
    34. }
    35.  
    36. IEnumerator DownloadFile ()
    37. {
    38.     isDownloadStarted = true;
    39.     GetWWW ();
    40.     Debug.Log ("Download started");
    41.     yield return (localWWW==null?null:localWWW);
    42.     Debug.Log ("Downlaod complete");
    43.     if (localWWW != null) {
    44.         if (string.IsNullOrEmpty (localWWW.error)) {
    45.             Debug.Log (localWWW.data);
    46.         }
    47.     }
    48. }
    49.  
    50. public void GetWWW ()
    51. {
    52.     localWWW = new WWW (@"http://www.sample.com");
    53. }
    But I am getting NullReferenceException: WWW class has already been disposed. TestScript+c__Iterator2.MoveNext () as exception.

    I am not sure what I am doing wrong here.

    Can anybody help me in this?

    Thanks