如何使用 c# 在 Unity3D 中实例化 GameObject 列表?我在检查器窗口中用预制件手动填充列表。
下面是我在 Deck.cs 中编写的代码,但我得到“对象引用未设置为对象的实例”。如果你有一个数组的解决方案,那也将不胜感激。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
namespace Assets
{
class Deck:MonoBehaviour
{
public List<GameObject> deck;
public void Fill()
{
GameObject c1 = Instantiate(deck[0]);
GameObject c2 = Instantiate(deck[1]);
GameObject c3 = Instantiate(deck[2]);
GameObject c4 = Instantiate(deck[3]);
GameObject c5 = Instantiate(deck[4]);
GameObject c6 = Instantiate(deck[5]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我也试过用一个数组来做,我得到“你想实例化的对象是空的”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
namespace Assets
{
class Deck:MonoBehaviour …Run Code Online (Sandbox Code Playgroud)