【问题标题】:Iteration with variable name [duplicate]使用变量名进行迭代[重复]
【发布时间】:2016-05-23 14:03:46
【问题描述】:

是否可以从 c# 中的变量进行迭代? 我有变量public Transform dest1, dest2, dest3, dest4,...public StudentScript stu1, stu2, stu3, stu4, ...;,我想要的是如果与student1 发生冲突,那么agent 会从stu1 到dest1 和callscript,如果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") {
        ...

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

【问题讨论】:

  • @NicholasV.:通过在线阅读、参加课程、寻找导师...... StackOverflow 不是一个教程网站。
  • @EricLippert 我同意这一点。希望他不要觉得一个问题不受欢迎,因为他认为这是初学者。

标签: c# unityscript


【解决方案1】:

是的。您想要的功能称为“数组”。数组是按整数位置索引的变量集合。

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

等等。

【讨论】:

  • 很久没有在这里看到 Eric Lippert 的回答了。除此之外,您还可以在 C# 中使用所谓的ListList 类似于 Array,但增加了一些功能,例如 AddRemoveClear 等。
猜你喜欢
  • 2012-09-24
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多