2016-05-23 97 views
1

是否有可能從c#中的變量進行迭代? 我有變數public Transform dest1, dest2, dest3, dest4,...public StudentScript stu1, stu2, stu3, stu4, ...;,我想的是,如果發生碰撞student1接着劑去Dest1處,並從屋頂盒callscript,如果STUDENT2然後dest2和屋頂盒2 ...變量名迭代

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

因爲我在這裏看到了Eric Lippert的答案,所以過了很長一段時間。除此之外,您也可以在C#中使用所謂的「List」。一個'List'類似於一個'Array',但是增加了一些功能,如Add,Remove,Clear等。 – GEEF