簡體   English   中英

在Unity C#中實例化游戲對象列表

[英]Instantiating a list of gameobjects in Unity C#

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

    }
}

考慮到您的評論,並且您提供的第一個腳本可以完美運行而沒有任何錯誤(應該這樣做:沒有理由應該返回錯誤),我覺得您是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.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;替換instanciatedObjects[i] = Instanciated(deck[i]) as GameObject; 通過instanciateObjects.Add(Instanciated(deck[i]) 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);
    }
}

用大寫字母命名列表也是一個好習慣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM