Trying to replace 2 objects. Destroying assets is not permitted to avoid data loss.
Hi, so i'm trying to replace 2 game objects, both in prefabs. Code kinda works even though the centers of the objects aren't aligned so they don't appear in the same place(would appreciate some input on that) but i keep getting the error message "Destroying assets is not permitted to avoid data loss" even though i'm not reffering to a prefab but a game object. Here's the code. using UnityEngine; using System.Collections;
public class DemoScript : MonoBehaviour {
public GameObject fullBox;
public GameObject destroyedBox;
public GameObject position;
GameObject createdFullBox;
// Use this for initialization
void Start () {
createdFullBox = (GameObject)Instantiate(fullBox, position.transform.position, Quaternion.identity);
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0))
{
Instantiate(destroyedBox, position.transform.position, Quaternion.identity);
Destroy(createdFullBox);
}
}
}
I have just run your code with two prefab cubes, put the script on the main camera, the position also being the camera, and this code ran just fine for me without error. I would assume then that you have code somewhere else that is trying to destroy a variable storing a prefab and not an instantiated object. I don't know what the objects you're using are, but if they're the same shape and size, setting both prefab's positions to the same values might solve the offset issue you're having.