2016-08-24 15 views
0

以下のコードで問題があります。それが何をしているのかは、一つの道筋を構成します。C#IEnumeratorは変更不可

currentLength = Random.Range(maxFloorLength - 2, maxFloorLength); 
    List<GameObject> tempList = new List<GameObject>(); 

    for (int i = 0; i < currentLength; ++i) 
    { 
     foreach (GameObject g in pooledFloor) 
     { 
      if (!g.activeInHierarchy) 
      { 
       g.SetActive(true); 
       g.transform.position = startPoint + buildDirection * i; 
       g.transform.DOScaleY(5, 0.25f).Play(); 
       lastFloorPositions.Add(g.transform.position); 
       tempList.Add(g); 
       break; 
      } 
     } 
     yield return new WaitForSeconds(0.05f); 
    } 

これは私がこれを変更するとき、期待通りに動作しますが:

currentLength = Random.Range(maxFloorLength - 2, maxFloorLength); 
    List<GameObject> tempList = new List<GameObject>(); 

    for (int i = 0; i < currentLength; ++i) 
    { 
     foreach (GameObject g in pooledFloor) 
     { 
      if (!g.activeInHierarchy) 
      { 
       g.transform.position = startPoint + buildDirection * i; 
       lastFloorPositions.Add(g.transform.position); 
       tempList.Add(g); 
       break; 
      } 
     } 
    } 

    foreach (GameObject g in tempList) 
    { 
     g.SetActive(true); 
     g.transform.DOScaleY(5, 0.25f).Play(); 
     yield return new WaitForSeconds(0.05f); 
    } 

それがないすべては、道路の一部(tempListリストの最後にあるべきもの)を作成することです。私が望むのは、スクリプトは、他の何かをする前にリストにすべてのプレハブを追加するので、私はその間にいくつか追加することができます。関数をvoidに変更しようとしましたが、このコードの一部をienumeratorとして呼び出し、ループの種類を変更しましたが、何も動作していないように見えます。私はDebug.Logで1つ以上のプレハブをまだ見つけているかどうかを確認しましたが、それは有効ですが、そのうちの1つしかアクティブ化していません。それは明らかに何か私を賭け、しかし、私はちょうどそれを見ることができない:/

、それがどのように見えるか:

How its working

を、それが見てどうあるべきか:

How it should look

をするのを忘れました言及 - IEnumeratorのすべての内部

+0

ないように注意してください、多分あなたは/ http://gamedev.stackexchange.com/でこれを尋ねるべきだろうか? –

+0

最初のパスはネストされ、2番目のパスはネストされません。試してみてください: –

+0

@Kuniqは私の答えを確認します –

答えて

2

をあなたはg.SetActive(true);を呼び出すときに、それが評価されますので、forループの次の反復で同じオブジェクトに対してif条件が真で文句を言いません次のアクティブでないオブジェクトの場合はtempListに追加してください。

for loopのすべての反復で2番目のケースでは、アクティブではないため、同じオブジェクトg(最初の非アクティブオブジェクトはpooledFloor)の評価を続けます。

があなたの期待される動作を実現するために、私はあなたがpooledFloorfor-loopの代わりfor-eachを使用し、その非アクティブな場合は、リストからオブジェクトを削除することをお勧め。

このような何か:

for (int i = 0; i < currentLength; ++i) 
{ 
    for (int j = 0; j < pooledFloor.Count; j++) 
    { 
     GameObject g = pooledFloor[j]; 
     if (!g.activeInHierarchy) 
     { 
      g.transform.position = startPoint + buildDirection * i; 
      lastFloorPositions.Add(g.transform.position); 
      tempList.Add(g); 
      pooledFloor.Remove(g); // Removing it from list would solve the problem. 
      break; 
     } 
    } 
} 
+0

リストから除外することは、パフォーマンス上の問題はないと思いますが、主な問題はあなたが言ったことでした。私はゲームオブジェクトをオンにしなかったのです。私はちょうどSetActiveをボトムループからアッパーに移動してから、ボトムループのRendererコンポーネントを有効にしました。それはすべてをかなり解決しました^^本当にありがとうございました:)私はそれがそれと同じくらい簡単なことを知っていました – Kuniq

+0

私は助けになることを嬉しく思っています。私の答えを正しいものとして受け入れてください –

0

最初のパスはネストされ、2番目のパスはネストされていません。試してみてください。このようなことの

for (int i = 0; i<currentLength; ++i) 
{ 
    foreach (GameObject g in tempList) 
    { 
     g.SetActive(true); 
     g.transform.DOScaleY(5, 0.25f).Play(); 
    } 
    yield return new WaitForSeconds(0.05f); 

} 
0

思う:最初のコードで

public void passONE() 
    { 
     currentLength = Random.Range(maxFloorLength - 2, maxFloorLength); 
     List<GameObject> tempList = new List<GameObject>(); 

     for (int i = 0; i < currentLength; ++i) 
     { 
      foreach (GameObject g in pooledFloor) 
      { 
       if (!g.activeInHierarchy) 
       { 
        g.transform.position = startPoint + buildDirection * i; 
        lastFloorPositions.Add(g.transform.position); 
        tempList.Add(g); 
        break; 
       } 
      } 
     } 
     passTwo(currentLength, tempList); 

    } 
    private void passTwo(double currentLength,List<GameObject> tempList) 
    { 


     for (int i = 0; i < currentLength; ++i) 
     { 
      foreach (GameObject g in tempList) 
      { 
       g.SetActive(true); 
       g.transform.DOScaleY(5, 0.25f).Play(); 

      } 
      yield return new WaitForSeconds(0.05f); 
     } 


    } 
関連する問題