是否有可能得到游戏对象嵌套子从父与脚本
本文关键字:脚本 嵌套 对象 有可能 游戏 是否 | 更新日期: 2023-09-27 18:01:36
是否可以从Parent获得游戏对象嵌套子对象?
地图游戏对象如下:
ListProcess (ListProcessItem1) => item1 => 1 => ItemIcon
例如我使用以下代码:
itemslot = GameObject.FindGameObjectWithTag ("ListProcessItem1").gameObject;
GameObject item = itemslot.gameObject.transform.GetChild(0).gameObject;
GameObject child = item.gameObject.transform.GetChild(0).gameObject;
GameObject sitemImage = child.gameObject.transform.GetChild(0).gameObject;
当我运行代码时,我得到了一个错误:
UnityException: Transform child out of bounds
LakeMainProcess.generatePrize1 () (at Assets/script/fishing/LakeMainProcess.cs:48)
LakeMainProcess.Start () (at Assets/script/fishing/LakeMainProcess.cs:21)
注意:我只能到达游戏对象(1)。gameobject (ItemIcon)得到一个错误
我该如何修复它?
用这个替换你的chunk:
Debug.Log("A " + GameObject.FindGameObjectWithTag ("ListProcessItem1") == null);
var itemslot = GameObject.FindGameObjectWithTag ("ListProcessItem1").gameObject;
Debug.Log("B " + itemslot.gameObject.transform.GetChild(0) == null);
GameObject item = itemslot.gameObject.transform.GetChild(0).gameObject;
Debug.Log("C " + item.gameObject.transform.GetChild(0) == null);
GameObject child = item.gameObject.transform.GetChild(0).gameObject;
Debug.Log("D " + child.gameObject.transform.GetChild(0) == null);
GameObject sitemImage = child.gameObject.transform.GetChild(0).gameObject;
它会告诉你找不到什么对象。我打赌是FindGameObjectWithTag
,你确定这个对象有这个确切的标签吗?
或者更简单地使用:
var ico = GameObject.FindGameObjectWithTag("ListProcessItem1").transform.FindChild("item1/1/ItemIcon").GetComponent<WhateverYouNeed>();