如何让我的地形达到 self-delete?

How do I get my terrain to self-delete?

我在删除脚本中设置了这段代码,首先关闭:

var terrain = GetComponent(Rigidbody2D);

function Update ()
{
    if (terrain.position.x <= Camera.main.transform.position.x - 5)
    {
        Destroy(this.GameObject);
    }
}

这是屏幕的图片:

还有我的预制件:

我想要的是,当摄像机在预制件前面时,我想让它自己删除。但是,这是我得到的错误:

NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Component.GetComponent (System.Type type)
Deletion..ctor () (at Assets/Scripts/Deletion.js:3)
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
Repeating Terrain:SpawnTerrain(Object) (at Assets/Scripts/Repeating Terrain.js:60)
Repeating Terrain:Update() (at Assets/Scripts/Repeating Terrain.js:52)

用计时器试试,然后用 console/debug 确保你有正确的对象

 Destroy (this.GameObject, 5);

或 GameObject.Destroy(游戏对象)

所以,事实证明,我只需要将 this.GameObject 替换为 this.gameObject。我把它大写了,仅此而已。