我做了一个脚本,它从我的Resources/ai文件夹加载预制板,循环最多5次,并生成一个随机的索引数。它成功地将我的预制板加载为GameObjects,并根据列表的计数生成随机索引。
但是,我在将预置文件发送到我的场景时遇到了麻烦,因为Instatiate命令在我编码时确实返回了一个错误。
我已经尝试过通过list[index].name甚至list[index].toString()获取预制件的名称,但是它是行不通的。(注意:list表示列表中的变量,index只是获取列表中游戏对象的索引。这不是实际的代码)。
如何用列表将加载的GameObject预制件移植到我的场景中?
我现在的代码是:
public List<GameObject> ai;
public GameObject[] spawn;
int indexAI;
int indexSpawn;
private void Start()
{
var res = Resources.LoadAll<GameObject>("ai/");
foreach (GameObject obj in res)
{
ai.Add(obj);
}
for (int x=0; x<5; x++)
{
indexAI = UnityEngine.Random.Range(0,ai.Count);
indexSpawn = UnityEngine.Random.Range(0, spawn.Length);
string name = ai[indexAI].name;
Debug.Log(name);
//I am currently using this kind of format since this is what I know for now.
Instantiate(name,spawn[indexSpawn].transform.position,spawn[indexSpawn].transform.rotation);
}谢谢!
发布于 2017-09-07 15:37:28
您正在尝试实例化一个字符串--您需要实例化ai列表中的游戏对象。
Instantiate(ai[indexAI] ,spawn[indexSpawn].transform.position, spawn[indexSpawn].transform.rotation);https://stackoverflow.com/questions/46099304
复制相似问题