2016-05-23 5 views
1

C#の変数から反復を行うことはできますか? 私は、変数を持っているpublic Transform dest1, dest2, dest3, dest4,...public StudentScript stu1, stu2, stu3, stu4, ...;、その後エージェントは、DEST1およびSTU1からcallscriptに行くSTUDENT1と衝突した場合に何がしたいことはあるかstudent2、その後DEST2とSTU2 ...この初心者のような質問をするための 変数名の反復

if (Physics.Raycast (ray, out hit)) { 
    if (hit.collider.name == "_student1") { 
     Debug.Log (hit.transform.name); 
     agent.SetDestination (dest1.position); 
     if (Mathf.Abs (tagent.position.x - dest1.position.x) <= closeEnough && Mathf.Abs (tagent.position.z - dest1.position.z) <= closeEnough) { 
      stu1.ResetStudentBehaviour(); 
     } 
    }else if (hit.collider.name == "_student2") { 
     agent.SetDestination (dest2.position); 
     if (Mathf.Abs (tagent.position.x - dest2.position.x) <= closeEnough && Mathf.Abs (tagent.position.z - dest2.position.z) <= closeEnough) { 
      stu2.ResetStudentBehaviour(); 
     } 
    }else if (hit.collider.tag == "_student3") { 
     ... 

申し訳ありません

+0

@NicholasV:オンラインで読んで、コースを取って、メンターを見つける... StackOverflowはチュートリアルサイトではありません。 –

+0

@EricLippert私はそれを搭載しています。彼はそれが初心者だと考えているので、質問が歓迎されていないように感じることがないように彼を求めた。 –

答えて

4

はい。必要な機能を「配列」と呼びます。配列はであり、整数の位置によって索引付けされる変数の集合です。

public Transform[] destinations = new Transform[10]; 
... 
destinations[0] = whatever; 
whatever = destinations[9]; 

などとなる。

+0

エリック・リッペルトの答えを見たのはずっとずっと前です。これに加えて、C#で 'List'というものを使うこともできます。 'List'は' Array'と似ていますが、 'Add'、' Remove'、 'Clear'などの機能が追加されています。 – GEEF