尝试隐藏并显示一组带有标签的克隆对象时出现错误。克隆是动态的(位置)。我已切换到可见和可见的一些信息文本。现在,我想使用标签隐藏和取消隐藏游戏对象。 (“ windtag”)。还要尝试用箭头方法将那些游戏对象隐藏起来。
public GameObject[] test;
public void invisible(bool log)
{
test= GameObject.FindGameObjectsWithTag("windtag");
test.GetComponent<Renderer>().enabled = log;
}
public void clone()
{
Canvas newcanvas = Instantiate(canvas);
//Use .SetParent(canvasName,false)
Text cloneposition = Instantiate(shiposition, newpos);
Text clonewind = Instantiate(windspeedtext, newpos);
Text clonedlow = Instantiate(flowtext, newpos);
Text clonetemperature = Instantiate(temperaturetext, newpos);
newcanvas.transform.position = shipvalue.transform.position;
cloneposition.transform.SetParent(newcanvas.transform, false);
clonewind.transform.SetParent(newcanvas.transform, false);
clonedlow.transform.SetParent(newcanvas.transform, false);
clonetemperature.transform.SetParent(newcanvas.transform, false);
clonewind.gameObject.SetActive(false);
clonetemperature.gameObject.SetActive(false);
}
public void arrow(float[,] arrowdata)
{
for (int x = 0; x < arrowdata.GetLength(0); x++)
{
for (int y = 0; y < arrowdata.GetLength(1); y++)
{
if (grid[x,y] ==1)
{
if (arrowdata[x, y] == 5)
{
GameObject referenceArrow = Instantiate(Resources.Load("down")) as GameObject;
float posY = shipvalue.transform.position.y - 9f;
referenceArrow.transform.position = new Vector3(shipvalue.transform.position.x-0.5f, posY);
}
if (arrowdata[x, y] == 4)
{
GameObject referenceArrow = Instantiate(Resources.Load("top left")) as GameObject;
float posY = shipvalue.transform.position.y - 9f;
referenceArrow.transform.position = new Vector3(shipvalue.transform.position.x - 0.5f, posY);
}
if (arrowdata[x, y] == 3)
{
GameObject referenceArrow = Instantiate(Resources.Load("top right")) as GameObject;
float posY = shipvalue.transform.position.y - 9f;
referenceArrow.transform.position = new Vector3(shipvalue.transform.position.x - 0.5f, posY);
}
}
}
}
}
这是错误: 严重性代码说明项目文件行抑制状态 错误CS1061'GameObject []'不包含'GetComponent'的定义,并且找不到可以接受的扩展方法'GetComponent'接受类型为'GameObject []'的第一个参数(您是否缺少using指令或程序集引用? )Assembly-CSharp C:\ Users \ Skylarking \ Unity \ My First Game \ MyFirstGame \ Assets \ Scripts \ test.cs 391有效
答案 0 :(得分:1)
我认为您当前的问题是,有多个对象带有标签“ windtag”。因此,如果您使用该标签搜索GameObject,则会返回多个。产生一个GameObject数组而不是一个GameObject。
一个简单的解决方法是向所有GameObject添加不同的Tag,然后将该字符串赋予函数。
invisible(false, "tagexample1");
public void invisible(bool log, string tag)
{
test= GameObject.FindGameObjectWithTag(tag);
test.GetComponent<Renderer>().enabled = log;
}
或者您可以将GetComponent<Renderer>()
放入for循环中。隐藏/显示带有标签的所有元素。如果那是您想要的。
public void invisible(bool log)
{
test= GameObject.FindGameObjectsWithTag("windtag");
foreach (GameObject gm in test){
gm.GetComponent<Renderer>().enabled = log;
}
}