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? ><
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…
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);
}
}