在Unity C中实例化游戏对象列表#

时间:2017-06-22 13:38:47

标签: c# list unity3d gameobject

如何使用c#在Unity3D中实例化GameObject列表? 我在检查员窗口中手动填充预制件列表。

enter image description here

下面是我在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]);
        }

    }
}

我也尝试用数组做,我得到'你要实例化的对象是空'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

namespace Assets
{
    class Deck:MonoBehaviour
    {
        public GameObject card1;
        public GameObject card2;
        public GameObject card3;
        public GameObject card4;
        public GameObject card5;
        public GameObject card6;

        public GameObject[] deck;

        public void Start ()
        {
            deck = new GameObject[5];
            GameObject c1 = Instantiate(card1) as GameObject;
            GameObject c2 = Instantiate(card2) as GameObject;
            GameObject c3 = Instantiate(card3) as GameObject;
            GameObject c4 = Instantiate(card4) as GameObject;
            GameObject c5 = Instantiate(card5) as GameObject;
            GameObject c6 = Instantiate(card6) as GameObject;
        }

    }
}

2 个答案:

答案 0 :(得分:3)

考虑到您的评论以及您提供的第一个脚本完美无误(因为它应该:没有理由它应该返回错误),我觉得您对Unity非常陌生。

因此,我建议您先阅读Unity教程(它们制作精良,可以帮助您了解引擎的基础知识)。您可以找到Roll-a-ball tutorial here

关于您的问题:

1 - 在第一个脚本中,Fill()方法无法在任何地方调用,您必须执行以下操作:

private void Start()
{
    Fill();
}

2 - Start()方法来自MonoBehaviour父类(以及每帧调用的Update()方法)并且被调用一次在场景的开头。期待this chart了解如何统一流动。

3 - 使用List<GameObject>GameObject[]全部取决于您的情况:如果要更改集合的大小,请转到列表。否则建议使用数组。

4 - 考虑到您的脚本我的猜测应该是这样的:

namespace Assets
{
    class Deck : MonoBehaviour
    {
        [SerializeField]
        private GameObject[] deck;

        private GameObject[] instanciatedObjects;

        private void Start()
        {
            Fill();
        }

        public void Fill()
        {
            instanciatedObjects = new GameObject[deck.Length];
            for (int i = 0; i < deck.Lenght; i++)
            {
                instanciatedObjects[i] = Instanciate(deck[i]) as GameObject;
            }
        }
    }
}

如果需要,您当然可以使用列表:)

修改
如果您想使用列表,您只需:

  • private GameObject[] instanciatedObjects;更改为private List<GameObject> instanciatedObjects;
  • instanciatedObjects = new GameObject[deck.Length];替换为instanciatedObjects = new List<GameObject>();
  • instanciatedObjects[i] = Instanciated(deck[i]) as GameObject;替换为instanciateObjects.Add(Instanciated(deck[i]) as GameObject);

希望这有帮助,

答案 1 :(得分:1)

使用foreach在预制件上循环使用预制件,如下所示:

public List<GameObject> Deck = new List<GameObject>(); // im assuming Deck is your list with prefabs?
public List<GameObject> CreatedCards = new List<GameObject>();

void Start()
{
     Fill();
}

public void Fill()
{
    foreach(GameObject _go in Deck)
    {
        GameObject _newCard = (GameObject)Instantiate(_go);
        CreatedCards.Add(_newCard);
    }
}

用大写字母命名你的名单也是一个好习惯。