Skip to main content
 首页 » 编程设计

c#之带变量名的迭代

2024年02月05日13daizhj

<分区>

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

很抱歉问了这个像初学者一样的问题