带变量名的迭代 [英] Iteration with variable name

查看:31
本文介绍了带变量名的迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 c# 中从变量进行迭代?我有变量 public Transform dest1, dest2, dest3, dest4,...public StudentScript stu1, stu2, stu3, stu4, ...; 我想要的是如果与 student1 发生碰撞,则代理从 stu1 转到 dest1 和 callscript,如果与 student2 发生碰撞,则转到 dest2 和 stu2...

Is it possible to make iteration from variables in c#? i have variables public Transform dest1, dest2, dest3, dest4,... and public StudentScript stu1, stu2, stu3, stu4, ...;and what i want is if collide with student1 then agent go to dest1 and callscript from stu1, if student2 then dest2 and 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") {
        ...

抱歉问了这个初学者般的问题

sorry for asking this beginner-like question

推荐答案

是的.您想要的功能称为数组".数组是由整数位置索引的变量集合.

Yes. The feature you want is called an "array". An array is a collection of variables that is indexed by an integer position.

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

等等.

这篇关于带变量名的迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆