需求:有三条路线(路线是由Transform对象组成的Transform数组),随机的取其中一条路线。
思路:把路线放入集合,对集合元素的数字索引做随机,以实现集合元素的随机
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomRoad : MonoBehaviour
{
public Transform[] paths0;
public Transform[] paths1;
public Transform[] paths2;
public List<Transform[]> pathList;
void Start ()
{
pathList.Add(paths0);//报错处
pathList.Add(paths1);
pathList.Add(paths2);
int i = Random.Range(0, 3);
Debug.Log(pathList[i]);
}
}
踩坑: “pathList.Add(paths0);”这个语句老是显示缺参数,起初我以为是paths0这个Transform数组的问题,就用for循环把里面的元素进行了遍历,结果都能打印出来——接着我想,是不是pathList集合的问题,就Debug.Log(pathList),此语句报错,但检查单词拼写,方法调用,都没有错误啊,纠结了一段时间后,决定查查网上别人是怎么用List泛型集合的——最后发现是,自己忘了对它进行初始化——之前用的时候也没太注意这个地方,今天掉坑里了