- Home /
Destroying assets is not permitted to avoid data loss.
var BombPrefab : Transform;
function Update() { if (Input.GetKeyDown("x")) { PlantBomb(); } }
function PlantBomb() { var bombPos = transform.position + (transform.forward * 2); Instantiate(BombPrefab,bombPos,Quaternion.identity); yield WaitForSeconds(2); Explode(); Destroy(BombPrefab.gameObject); }
/^can you correct this code because it is displaying the error - Destroying assets is not permitted to avoid data loss. */
Answer by Neten · Nov 16, 2011 at 03:14 AM
I believe that this should do the trick:
var BombPrefab : Transform;
function Update() { if (Input.GetKeyDown("x")) { PlantBomb(); } }
function PlantBomb()
{
var bombPos = transform.position + (transform.forward * 2);
var cloneBomb=Instantiate(BombPrefab,bombPos,Quaternion.identity);
yield WaitForSeconds(2);
Explode();
Destroy(cloneBomb);
}
What I did was assign a variable that would hold the instantiated,(clone) object and instead of destroying the Prefab, I destroyed the cloned object.
Thanks so much! Helped me out big time! Didn't realise it was trying to destroy the prefab itself!!!
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)){
Vector3 direction = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position).normalized;
GameObject cloneHook = Instantiate (hook, transform.position , Quaternion.identity) as GameObject;
cloneHook.GetComponent<Rigidbody2D>().AddForce (direction * speed);
}
Vector3 pudgePos = transform.position;
Vector3 hookPos = hook.transform.position;
float distance = Vector3.Distance (pudgePos, hookPos);
if(distance >= 3f)
{
Destroy(cloneHook);
}
I was having the same issues so I tried this, but it says 'the name 'clone hook' does not exist in this context' and if I put all the code inside the first if statement, the hook does not destroy after travelling the distance. What do I do??
Answer by GamezAtWork · Sep 07, 2011 at 08:31 AM
You have to create a variable to store the instantiated GameObject.
Like...
var theBomb:GameObject = Instantiate(blah blah blah) Destroy(theBomb)
Because right now, you are destroying the prefab inside the library itself.
it displays - Cannot convert 'UnityEngine.Transform' to 'UnityEngine.GameObject'.
i've done all kinds of stuff like that before but it still gives errors..
btw, this script is connected to $$anonymous$$ain Camera and the Bomb prefab..
You need to cast it to a gameobject, just ad "as GameObject" to the end of the instantiate statement.
Answer by ariesastra · Sep 24, 2013 at 06:27 AM
i have this code :
var explosion : GameObject;
var explosionSound : GameObject;
function Update () {
}
function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == "Laser"){
Instantiate (explosion, transform.position, transform.rotation);
Instantiate (explosionSound, transform.position, transform.rotation);
Destroy(other.gameObject);
Destroy (gameObject);
}
}
and alwasy get this error : Destroying assets is not permitted to avoid data loss.
If you really want to remove an asset use DestroyImmediate (theObject, true); UnityEngine.Object:Destroy(Object);
please anyone can fix this :(
Answer by jtig3d · Apr 14, 2019 at 05:29 AM
GameObject cloneExplosion = Instantiate (explosion, transform.position, transform.rotation);
GameObject cloneSound = Instantiate (explosionSound, transform.position, transform.rotation);
Destroy(cloneExplosion , 3.0f); Destroy(cloneSound , 3.0f);
Answer by zahaider14 · Oct 08, 2021 at 06:57 PM
I also tried to solve this issue by passing the prefab into variable and I faced the same issue. Then I've seen that the instantiated gameobject gets destroyed or enabled false before the "Destroy (exampleGameobject)" command. Then I tried the command like this: If(exampleGameObject.activeInHierarchy) Destroy (exampleGameObject)
Then I didn't received any kind of LogError like this.
Your answer
Follow this Question
Related Questions
Delete children of Instantiated object 2 Answers
instantiate,destroy,gain speed in time 2 Answers
Can't remove instantiated prefab 0 Answers
I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers
How to instantiate an object BEHIND my current position? 1 Answer