2017-06-22 82 views
1

如何在Unity3D中使用c#實例化一個GameObject列表? 我在巡視窗口中手動填充預製件列表。在Unity C中實例化一個gameobjects列表#

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

    } 
} 
+0

在你得到了什麼路線錯誤消息?這6行對我來說似乎沒有問題(即使我們不知道何時調用Fill()方法)。 – Kardux

+0

在'公開名單甲板' –

+0

不確定是否引發此錯誤:我測試了它,沒有發生錯誤。查看我的答案獲取更多詳情。 – Kardux

回答

3

那麼考慮您的意見和第一腳本您提供完美的作品沒有任何錯誤(因爲它應該:有沒有道理,它應該返回一個錯誤),我覺得你是很新的統一。

因此,我建議您在做任何其他事情之前查看Unity教程(它們製作得非常好,可以幫助您瞭解引擎的基本知識)。你可以找到Roll-a-ball tutorial here

關於你的問題:

在第一個腳本,該Fill()方法沒有任何地方叫,你必須做這樣的事情:

private void Start() 
{ 
    Fill(); 
} 

2-Start()方法來自MonoBehaviour父類(以及Update()方法,它被稱爲每一幀),並在場景開始時被調用一次。看看this chart瞭解如何製造團結流。

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

當然你也可以在需要時:)

編輯仍然使用清單:
如果您想要使用你只需要的清單:

  • cha NGE private GameObject[] instanciatedObjects;private List<GameObject> instanciatedObjects;
  • 通過instanciatedObjects = new List<GameObject>();
  • 更換instanciatedObjects = new GameObject[deck.Length];取代instanciatedObjects[i] = Instanciated(deck[i]) as GameObject;通過instanciateObjects.Add(Instanciated(deck[i]) as GameObject);

希望這有助於

+0

感謝您的回答。我也會嘗試你的代碼版本。是的,我對Unity非常陌生:) –

+0

注意到了這一點,這就是爲什麼我想盡可能多地提供信息:只要你花時間去學習Unity,Unity的學習速度就會很快。因爲它感覺就像你熟悉C#那麼它不應該花很長時間才能掌握它:) – Kardux

+0

我試過你的解決方案,它完美的工作!在列表的情況下,當我嘗試通過索引調用實例化對象時,出現超出範圍異常。該陣列雖然工作得很好。非常感謝你的幫助,我終於做了我想要的好幾個小時。 –

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

此外,用大寫字母命名您的列表是個好習慣。

+0

它不會給出任何錯誤,但它也不會創建任何實例。 :( The CreatedCards列表一直是空的 –

+0

是你的Deck充滿了GameObjects嗎?另外檢查編輯 - 我忘記了新列表() – Fiffe

+0

不行,如果我調用函數Start而不是Fill –