用变量名迭代

时间:2016-05-23 14:03:47

标签: c# unityscript

是否可以从c#中的变量进行迭代? 我有变量public Transform dest1, dest2, dest3, dest4,...public StudentScript stu1, stu2, stu3, stu4, ...;我想要的是如果与student1发生碰撞然后代理从stu1转到dest1和callcript,如果是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") {
        ...

抱歉问这个类似初学者的问题

1 个答案:

答案 0 :(得分:4)

是。您想要的功能称为&#34;数组&#34;。数组是由整数位置索引的变量集合。

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

等等。