5秒后错误重生预制件?

时间:2015-01-06 22:06:33

标签: unity3d unityscript

所以这是我的脚本,我得到错误(转换)' MissingReferenceException:类型'转换'已被破坏,但你仍然试图访问它。您的脚本应该检查它是否为null或者您不应该销毁该对象。 UnityEngine.Transform.get_position()(在C:/ BuildAgent / work / aeedb04a1292f85a / artifacts / EditorGenerated / UnityEngineTransfo rm.cs:28)' -

#pragma strict

var objectToSpawn : GameObject;

function Update () {
    if (Input.GetMouseButtonDown(0)) {
    var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit : RaycastHit;
        if (Physics.Raycast (ray, hit)) {
           if (hit.collider.tag == "destroyable") {
               var oldTransform = hit.collider.gameObject.transform;
               Destroy(hit.collider.gameObject);
               StartCoroutine(SpawnAfter5Seconds(oldTransform));
            }
         }
    }
}

function SpawnAfter5Seconds(oldTransform:Transform)
{
       yield WaitForSeconds (5);
       var newObject = Instantiate (objectToSpawn , oldTransform.position, oldTransform.rotation);
}

1 个答案:

答案 0 :(得分:1)

您正在使用旧对象(已销毁)来选择生成对象的位置。相反,将位置和旋转保存到变量中,并将其传递给函数SpawnAfter5Seconds而不是null变换。