[英]How to get the value of gameobject array from one script to another?
我是 c# 和 arcore 的新手。 我有一個名為 GUIController.cs 的示例控制器腳本和另一個名為 ItemScrollList.cs 的項目列表。 我在 GUIController 中制作了一個帶有數組的游戲對象,其中包含 2 個預制元素 0 和 1。我想要實現的是,每次我單擊 itemscrollList 的內容(在本例中為 2 個項目,即 item1 和 item2)我需要更改GUIController 上的預制件。
我從下面的代碼中得到的錯誤是,每次我單擊內容項 1 或 2 時,它們都會返回 nullpointerexceptions。
GUIController.cs 上的行“AndyPrefab = nowPrefabs.GetComponent().currentPrefabs;” 是我需要它來正確顯示預制件的行。 請幫我
圖形控制器.cs
if (hit.Trackable is FeaturePoint)
{
AndyPrefab = AndyPlanePrefab[0];
}
else
{
//this is manual input value AndyPlanePrefab as array 1
//AndyPrefab = AndyPlanePrefab[1];
//this is where i need it to properly show the correct prefab
AndyPrefab = nowPrefabs.GetComponent<ItemScrollList>().currentPrefabs;
}
ItemScrollList.cs
//this is where i create the button for itemlist
private void AddButtons()
{
for (int i = 0; i < itemList.Count; i++)
{
Item item = itemList[i];
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.SetParent(contentPanel);
ItemButton itemButton = newButton.GetComponent();
itemButton.Setup(item, this);
}
}
//this is where im trying to change the prefabs and connect it to
GUIController, if i click on item 1 it should change the gameobject[] value
to AndyPrefab.
public void TryToChangePrefabs(Item item)
{
if (item == itemList[0]) {
currentPrefabs = changePrefabs.GetComponent().AndyPlanePrefab[0];
Debug.Log("Condition number 1 done");
}
else if (item == itemList[1])
{
currentPrefabs = changePrefabs.GetComponent().AndyPlanePrefab[1];
Debug.Log("Condition number 2 done");
}
}
您需要在要從中訪問其他腳本的腳本中創建一個游戲對象變量,然后將游戲對象與您要訪問的腳本一起拖放到游戲對象變量中才能訪問它,您必須這樣做
mygameobjectvariable.getComponent<myscriptname>().myarray[1]
在創建按鈕的方法中,可以鏈接整個過程:
private void AddButtons()
{
for (int i = 0; i < itemList.Count; i++)
{
int temp = i;
Item item = itemList[i];
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.SetParent(contentPanel);
newButton.GetComponent<Button>().onClick.AddListener(()=>{
currentPrefabs = item;
});
}
}
我不完全確定該方法中將發生什么,但是,您知道,您可以傳遞項目和臨時項目,以便您可以訪問循環和索引中的當前項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.