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

Instantiating a list of gameobjects in Unity C#

如何使用 C# 在 Unity3D 中实例化游戏对象列表? 我在检查器 window.

中手动用预制件填充列表

下面是我在 Deck.cs 中编写的代码,但我得到 "Object reference is not set to an instance of an object"。如果您有一个数组的解决方案,我们也将不胜感激。

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

    }
}

我也尝试用数组来做,我得到 'The object you want to instantiate is null'

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

    }
}

使用 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);
    }
}

用大写字母命名列表也是一个好习惯。

好吧,考虑到您的意见,并且您提供的第一个脚本可以完美无误地工作(应该如此:没有理由 return 出现错误),我觉得您对 Unity 还很陌生。

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

关于您的问题:

1- 在第一个脚本中,Fill() 方法没有在任何地方被调用,你必须这样做:

private void Start()
{
    Fill();
}

2- 这个 Start() 方法来自 MonoBehaviour 父 class (以及 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);

希望对您有所帮助,