Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何引用子元素中的图像组件?_C#_Unity3d - Fatal编程技术网

C# 如何引用子元素中的图像组件?

C# 如何引用子元素中的图像组件?,c#,unity3d,C#,Unity3d,我有一个预制件: 网格空间和图像都有图像组件。GridSpace的图像组件用于存储按钮颜色,图像的图像组件用于存储图片。 接下来,我将向场景中添加几个网格空间预设,但我希望每个图像元素都具有不同的随机图像。我使用此命令添加预制元素,并将阵列元素与已创建对象的组件链接: for (int y = 0; y < gridsInRow; y++) { for (int x = 0; x < gridsInColumn; x++)

我有一个预制件:

网格空间和图像都有图像组件。GridSpace的图像组件用于存储按钮颜色,图像的图像组件用于存储图片。 接下来,我将向场景中添加几个网格空间预设,但我希望每个图像元素都具有不同的随机图像。我使用此命令添加预制元素,并将阵列元素与已创建对象的组件链接:

  for (int y = 0; y < gridsInRow; y++)
        {
            for (int x = 0; x < gridsInColumn; x++)
            {
                GameObject newSmoke = Instantiate(gridSpacePrefab, new Vector3(0, 0, 0), Quaternion.Euler(0, 0, 0)) as GameObject;
                buttonTextArray[x, y] = newSmoke.GetComponentInChildren<Text>();
                imageArray[x, y] = newSmoke.GetComponentInChildren<Image>();
            }
        }
for(int y=0;y
我想让imageArray存储对图像的图像组件的引用,但它似乎引用了网格空间元素中的图像组件。 你知道上面的代码有什么问题吗?或者我如何引用image元素中的image组件? 此外,buttonTextArray在文本元素中存储对文本组件的引用。唯一的问题是这个图像组件。

还查看当前对象(不仅是其子对象)的内部,因此它返回网格空间的图像组件

您可以这样做:

imageArray[x, y] = newSmoke.transform.Find("Image").GetComponent<Image>();
imageArray[x,y]=newSmoke.transform.Find(“Image”).GetComponent();
但是名称是硬编码的,因此如果更改“图像”名称,也应该在代码中更改它。另一种方式是:

imageArray[x, y] = newSmoke.GetComponentsInChildren<Image>()[1];
imageArray[x,y]=newSmoke.GetComponentsInChildren()[1];
只有“网格空间”和“图像”游戏对象有图像组件。请注意GetComponents子对象中的“s”。

还查看当前对象(不仅是其子对象)的内部,因此它返回网格空间的图像组件

您可以这样做:

imageArray[x, y] = newSmoke.transform.Find("Image").GetComponent<Image>();
imageArray[x,y]=newSmoke.transform.Find(“Image”).GetComponent();
但是名称是硬编码的,因此如果更改“图像”名称,也应该在代码中更改它。另一种方式是:

imageArray[x, y] = newSmoke.GetComponentsInChildren<Image>()[1];
imageArray[x,y]=newSmoke.GetComponentsInChildren()[1];
只有“网格空间”和“图像”游戏对象有图像组件。注意儿童中GetComponents中的“s”