0

我想销毁我实例化的游戏对象,当我尝试这样做时遇到很多错误,例如“名称克隆在当前上下文中不存在”,“无法将对象表达式转换为 UnityEngine.Object 类型”。我尝试了很多我在网上找到的东西,但没有任何帮助。这是我的代码:

if(distance<renderDistance)
    {
        if(!temp)
        {
            GameObject clone = Instantiate(chunk,transform.position,transform.rotation)as GameObject;
            temp = true;
        }
    }
    else
    {
        Destroy(clone);
    }
4

1 回答 1

2

您收到“名称克隆在当前上下文中不存在”错误,因为您在“if(!temp)”括号内声明了此变量('clone'),并且在关闭括号后它不存在。

试试这个代码:

GameObject clone = null;
if (distance < renderDistance)
{
    if(!temp)
    {
        clone = (GameObject)Instantiate(chunk, transform.position, transform.rotation);
        //be sure 'chunk' is GameObject type
        temp = true;
    }
}
else
{
    if (clone != null)
        Destroy(clone);
}

如果您有任何问题或需要更多帮助,请告诉我。

于 2014-07-29T21:42:44.583 回答