【问题标题】:unity Destroy instantiated GameObjectunity 销毁实例化的游戏对象
【发布时间】:2014-07-29 08:25:59
【问题描述】:

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

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

【问题讨论】:

    标签: unity3d instantiation destroy gameobject


    【解决方案1】:

    您收到“名称克隆在当前上下文中不存在”错误,因为您在“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);
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      相关资源
      最近更新 更多