如何将gameObjects放入列表?

时间:2012-11-22 09:26:32

标签: c# list unity3d instantiation gameobject

我有gameObjects,我需要把它们放到列表中,类似的东西:

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

4 个答案:

答案 0 :(得分:1)

unityGameObjects.Add(new GameObject());

答案 1 :(得分:1)

首先创建列表。

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

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

寻找GameObject

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

创建新实例

您无法在GameObjects上调用new,因为它们是MonoBehaviours,但您可以从预制件中实例化它们。

GameObject g = Instantiate(prefab);
unityGameObjects.Add(g);

答案 2 :(得分:0)

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

 unityGameObjects.Add(GameObject.Find(unityObject.Value));

答案 3 :(得分:0)

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" } } );