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

问题描述 投票:0回答:2

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

    }
}

我也尝试用数组来做,我得到“你想要实例化的对象为空”

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

    }
}
c# list unity-game-engine gameobject
2个回答
6
投票

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

因此,我建议您首先查看 Unity 教程(它们制作得非常好,将帮助您了解引擎的基础知识)。您可以在这里找到滚球教程

关于你的问题:

1- 在第一个脚本中,

Fill()
方法没有在任何地方被调用,你必须这样做:

private void Start()
{
    Fill();
}

2- 这个

Start()
方法来自
MonoBehaviour
父类(以及每帧调用的
Update()
方法),并在场景开始时调用一次。查看此图表了解统一流程是如何形成的。

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.Length; 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);

希望这有帮助,


2
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.