Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by j___a___y · Sep 07, 2011 at 08:28 AM · instantiatetransformvector3destroymain camera

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. */

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image POLYGAMe · Jan 25, 2012 at 11:22 PM 0
Share

Thanks so much! Helped me out big time! Didn't realise it was trying to destroy the prefab itself!!!

avatar image Dani07 · Feb 26, 2013 at 11:09 AM 0
Share

Thanks Neten, It helped me as well.

avatar image theomkarpatil · Jan 06, 2017 at 06:56 PM 0
Share
 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??

avatar image
4

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image j___a___y · Sep 07, 2011 at 08:38 AM 0
Share

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..

avatar image raybarrera · Nov 16, 2011 at 01:10 AM 1
Share

You need to cast it to a gameobject, just ad "as GameObject" to the end of the instantiate statement.

avatar image
1

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 :(

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

problem whit Instantiate Prefabs position. 0 Answers

How do I duplicate an object to work with a shootemup 0 Answers

Delete children of Instantiated object 2 Answers

instantiate,destroy,gain speed in time 2 Answers

Can't remove instantiated prefab 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges