2017-02-13 9 views
1

私はこのエラーを取得する:Unity ArgumentOutOfRangeException:引数が範囲外です。パラメータ名:I

ArgumentOutOfRangeException: Argument is out of range. 
Parameter name: i 
    System.Text.RegularExpressions.MatchCollection.get_Item (Int32 i) 
    ServerTime.SplitString (UnityEngine.WWW www) (at Assets/Script/Common/ServerTime.cs:48) 
    ServerTime+<GetTime>c__IteratorB.MoveNext() (at Assets/Script/Common/ServerTime.cs:31) 

エラーリンク:

time = string.Format("{0}:{1}:{2} {3}:{4}:{5}" 

マイコード:

using UnityEngine; 
using System.Collections; 
using System.Text.RegularExpressions; 

public class ServerTime : MonoBehaviour{ 

private string url = "http://www.beijing-time.org/time.asp"; 
private string time = string.Empty; 

public delegate void GetTimeBackCall(string time); 
private static GetTimeBackCall call; 

public void GetServerTime(GetTimeBackCall backCall) 
{ 
    call = backCall; 
    StartCoroutine("GetTime"); 

} 

public IEnumerator GetTime() 
{ 
    //Debug.Log("Start Requesting server time"); 

    while (true) 
    { 
     WWW www = new WWW(url); 
     yield return www;     //Blocked here, waiting for a response after the return 
     if (www.isDone && string.IsNullOrEmpty(www.error) && www.text.Length >= 10) 
     { 
      SplitString(www); 
      break; 
     } 
     //Debug.Log("Re-request the server time"); 
    } 

    yield return new WaitForFixedUpdate(); 
} 

private void SplitString(WWW www) 
{ 
    //Use regular matching expression 
    string patten = @"[0-9]{1,};"; 
    Regex regex = new Regex(patten); 
    MatchCollection result = regex.Matches(www.text); 

    //Time Organizational 
    time = string.Format("{0}:{1}:{2} {3}:{4}:{5}" 
         , result[0].Value.TrimEnd(';') 
         , result[1].Value.TrimEnd(';') 
         , result[2].Value.TrimEnd(';') 
         , result[3].Value.TrimEnd(';') 
         , result[4].Value.TrimEnd(';') 
         , result[5].Value.TrimEnd(';') 
         ); 


    if(time.Length >= 10) 
    { 
     call(time); 
    } 

    //Debug.Log("EU:" + time); 
} 
} 
+0

エラーから、私はそれが6未満であると思われるから – Dmihawk

答えて

0

あなたの問題は、SplitString()方法です。このメソッドはnull参照問題を含むあらゆる種類の潜在的な問題を抱えているので、このメソッドをクリーンアップすると長期的に役立ちます。

ここでは、特定の問題を解決する方法の例を示します。あなたの正規表現がどれだけ多くの要素を渡しているのか分からないので、6つあると仮定するのではなく、それらをループする方がよいでしょう。これは原理的にはうまくいくはずです。私はそれをテストせずにこれを入力した:

private void SplitString(WWW www) 
{ 
    // Use regular matching expression. 
    MatchCollection result = new Regex(@"[0-9]{1,};").Matches(www?.text ?? string.Empty); 

    // Time Organizational. (if you used a StringBuilder it could be a touch faster) 
    string time = string.Empty; 
    foreach (var item in result) // Loop through all results no matter how many. 
     time += time + item.Value.TrimEnd(';') + ":"; 
    time = time.TrimEnd(':'); // Get rid of the last unnecessary colon. 

    // Unsure what this does, but your original code contained it. 
    if(time.Length >= 10) 
     call(time); 
} 

あなたはユニットテストを書いた場合はPSあなたが問題を発見していた自分が;-)あなたの「時間= String.Formatの(...」行にブレークポイントを設定し

+0

返信をありがとう、私はこれを試してみます – max007

+0

私はこの行にエラーが表示されます:時間+ =時間+アイテム。 Value.TrimEnd( ';')+( ":"); System.Objectには 'Value'の定義が含まれていません。 – max007

関連する問題