5 秒错误后重生预制件?

Respawn Prefab After 5 Second's Error?

这是我的脚本,我收到错误 (Transform) 'MissingReferenceException: 'Transform' 类型的对象已被销毁,但您仍在尝试访问它。您的脚本应该检查它是否为空,或者您不应该销毁该对象。 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);
}

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