Destroying Assets Warning

Hi

My build is returning the following error:

“Destroying assets is not permitted to avoid data loss”

So I started reading into things like using Object Pool manager (http://forum.unity3d.com/viewtopic.php?t=36691&highlight=destroy).

Unity advises me to use DestroyImmediate(theObject,true); … but when I do this Unity tells me “Destroying object immediately is not permitted during physics trigger and contact callbacks. You must use Destroy instead”…

Obviously, very confusing for a noob like me.

Also, using either of the above, I can see that whilst the object has disappeared from the game screen, it is still listed in the hierachy… (The objects being destroyed are clone instances of a prefab if that makes any difference).

But none of them seem to be the solution… Is the above error problematic or should I just ignore it. If it’s not permitted, then why have it? ><

Thanks.

Perhaps you’re not setting the second argument to true?

…or not. You edited the post.

I don’t think you’re doing what you think you’re doing, however, if you’re getting that error.

I had this:

var enemy : GameObject;

function OnTriggerEnter (other : Collider) 
{
	DestroyImmediate(enemy, true);
	
}

Okay, right. Why are you using that instead of Destroy?

See my first post above - because Unity tells me to. When I use Destroy i get a "“Destroying assets is not permitted to avoid data loss” error message…

I don’t see that in your post.

Here, it works fine. My assumption is that you dragged a prefab onto the “Enemy” variable slot instead of an instance of it.

234916–8407–$destroy_147.unitypackage (5.82 KB)

Yes you’re right… (btw I can’t download that for some reason…)

This means the method I’m using to spawn enemies is problematic.

The code I’m using spawns clones of the prefab from a spawnpoint (empty object in the scene)… This code came from elsewhere in the forum:

var enemy : GameObject[]; 

//-----
function Start() { 
   Spawn(); 
} 
//-----SPAWN

yield WaitForSeconds(0.5); 

function Spawn() { 
   for (i=0;i<1;i++) { 
      
      yield WaitForSeconds(0.5); 
      
    Instantiate(enemy[Random.Range(0, enemy.Length)], transform.position, transform.rotation);
     Debug.Log("First enemy spawned");
     
//Give the cloned object an initial velocity along the current objects Y axis: (used to have clone. at start of next line)
	velocity = transform.TransformDirection(Vector3.forward*30);
   } 
   
   
   
  yield WaitForSeconds(5); 
   Destroy (GameObject());
   //Debug.Log("First enemy DESTROYED");
   
//spawn more 

    for (i=0;i<5;i++) { 
    yield WaitForSeconds(3); 
  //    Debug.Log("Next round of enemy");
    
     Instantiate(enemy[Random.Range(0, villagers.Length)], transform.position, transform.rotation);
   } 
}