So this is my script and I get the error (Transform) 'MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Transform.get_position () (at C:/BuildAgent/work/aeedb04a1292f85a/artifacts/EditorGenerated/UnityEngineTransform.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);
}