如何让我的地形自行删除?

时间:2016-04-09 02:55:41

标签: unity3d unityscript gameobject rigid-bodies

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

var terrain = GetComponent(Rigidbody2D);

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

这是一张屏幕图片:

enter image description here

我的预制材料:

enter image description here

我想要的是,当相机在预制件前面时,我希望它能够自行删除。但是,这是我得到的错误:

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)

2 个答案:

答案 0 :(得分:1)

事实证明,我只需要用this.gameObject替换this.GameObject。我把它资本化了,就是这样。

答案 1 :(得分:0)

尝试使用计时器并使用console / debug确保您拥有正确的对象

 Destroy (this.GameObject, 5);

或 GameObject.Destroy(游戏物体)