0

我有游戏对象,我需要将它们放入列表中,如下所示:

List<GameObject> unityGameObjects = new List<GameObject>();

4

4 回答 4

4

您首先创建您的列表。

List<GameObject> unityGameObjects = new List<GameObject>();

您需要引用要添加到列表中的 GameObject。您可以通过查找 GameObject 或创建它的新实例来完成此操作。

寻找游戏对象

GameObject g = GameObject.Find("NameOfGameObject");
unityGameObjects.Add(g);

创建新实例

您不能调用newGameObjects,因为它们是 MonoBehaviours,但您可以从预制件中实例化它们。

GameObject g = Instantiate(prefab);
unityGameObjects.Add(g);
于 2014-10-11T23:47:07.313 回答
1
unityGameObjects.Add(new GameObject());
于 2012-11-22T09:33:11.713 回答
1
MyList.AddRange( new List<GameObject> { new GameObject { name = "Frank", tag = "3" ,layer= 2, active = isActiveAndEnabled, hideFlags =  HideFlags.HideInHierarchy, isStatic = false },
                                       new GameObject { name = "Jill", tag = "3" },
                                       new GameObject { name = "Dave", tag = "5" },
                                       new GameObject { name = "Jack", tag = "8" },
                                       new GameObject { name = "Judith", tag = "12" },
                                       new GameObject { name = "Robert", tag = "14" },
                                       new GameObject { name = "Adam", tag = "1" } } );
于 2019-08-18T17:39:30.983 回答
0

我找到了我的问题的解决方案,因此我会问我的问题,也许它对其他人有帮助:

 unityGameObjects.Add(GameObject.Find(unityObject.Value));
于 2012-12-12T12:19:30.730 回答