Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by gump74 · Aug 22, 2013 at 06:16 AM · javascriptinstantiatedestroyunit

removing an instantiated prefab

I am close to finishing this step. far too many step left to count but that is another issue. I have been able to add a knife to my player however, I have not found the way to remove it. I will show a code snippet.

 var up:Transform;
 var dn:Transform;
 var knife:Transform;
 
 private var weapon:Transform = null;
 
 function FixedUpdate() {
     if (  Input.GetKeyDown(KeyCode.Alpha0) ) {
         SetMode(0);
         Destroy(knife);   // have also tried Destroy(weapon);
         weapon = null;
     }  
     else if (  Input.GetKeyDown(KeyCode.Alpha9) ) {
         SetMode(3);
         if ( weapon == null ) {
             weapon = new Instantiate(knife, Vector3(0, 0, 0), Quaternion.identity);
             weapon.transform.parent = dn.transform;
             weapon.transform.localPosition = Vector3(0, 0, 0);
             weapon.transform.localRotation = dn.localRotation;
         }
     } else if (  Input.GetKeyDown(KeyCode.Alpha8) ) {
         SetMode(4);
         if ( weapon == null ) {
             weapon = new Instantiate(knife, Vector3(0, 0, 0), Quaternion.identity);
             weapon.transform.parent = up.transform;
             weapon.transform.localPosition = Vector3(0, 0, 0);
             weapon.transform.localRotation = up.localRotation;
         }
     }
 }

  


the weapon appears as expected when I press 8 or 9 but when I press 0 the script errors. the error when Destroy(knife); is:

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) PlayerAnimation:FixedUpdate() (at Assets/manikin/PlayerAnimation.js:44)

that would destroy the prefab from the project right?

the error for Destroy(weapon); is:

Can't destroy Transform component. If you want to destroy the game object please call 'Destroy' on the game object instead. Destroying the transform component is not allowed.

I think I am close but I don't see anything in the unity doc that helps, though it is big so I could have missed it. Does any know the problem?

Comment
Add comment · Show 1
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 gump74 · Aug 22, 2013 at 06:19 AM 0
Share

oh, and I tried making both knife and weapon GameObject types howevr, I get this error:

InvalidCastException: Cannot cast from source type to destination type. PlayerAnimation.FixedUpdate () (at Assets/manikin/PlayerAnimation.js:58)

1 Reply

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

Answer by whydoidoit · Aug 22, 2013 at 06:22 AM

You certainly don't want to Destroy knife as that's the prefab - what you actually need is to destroy the GameObject of the instance you've made in weapon:

     Destroy(weapon.gameObject);
Comment
Add comment · Show 5 · 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 gump74 · Aug 22, 2013 at 06:26 AM 0
Share

Thank you for your quick reply however, it produced this error:

NullReferenceException: Object reference not set to an instance of an object PlayerAnimation.FixedUpdate () (at Assets/manikin/PlayerAnimation.js:45)

avatar image gump74 · Aug 22, 2013 at 06:30 AM 0
Share

do I need something like?:

 weapon.AddComponent = new Instantiate(knife, Vector3(0, 0, 0), Quaternion.identity);
avatar image whydoidoit · Aug 22, 2013 at 06:34 AM 0
Share

No you don't need that (and indeed it would not compile).

Your code should be in Update not FixedUpdate - because FixedUpdate is not for Input and is for physics. FixedUpdate can run multiple times per frame and so your code to destroy things could run many times in a frame.

I also suggest you do

      if(weapon != null) Destroy(weapon.gameObject).
avatar image gump74 · Aug 22, 2013 at 06:46 AM 0
Share

Sweet works like a charm, I'll move that code to the regular update. Thank you so much!!!

avatar image cdunne1 · Jul 19, 2017 at 02:34 PM 0
Share

I tried to use that code and it didn't work :/

I am calling the function from within the void Update. I include the code below and would seriously appreciate any help! I have a feeling I am close... just not close enough!

 void Update () 
 {
     IsFailed (); //Check constantly if the player is winning
     Play (); //Launch the level on click
     loopDone = false;
     PausePlay();
 }

 void PausePlay()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P))            //p button as pause input
     {
         if (Time.timeScale == 1)
         {
             Time.timeScale = 0;                 //pause that bitch and show the tagged PauseScreen objects
             showPaused();
             }
         else if (Time.timeScale == 0)
         {
             Time.timeScale = 1;
             hidePaused();                       //hide showonpause objects
         }
     }
 } 


 public void showPaused()                            //shows objects with ShowOnPause tag - when in canvas
 {
     GameObject prefab = Instantiate(Resources.Load<GameObject>("Pause$$anonymous$$enu")); //Load prefab   }

 public void hidePaused()                            //hides objects with ShowOnPause tag
  {
     Destroy(prefab.gameObject);    //also tried with Destroy(Pause$$anonymous$$enu.gameObject);
 }

 
 

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

17 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 avatar image avatar image avatar image

Related Questions

Check if child exists and instantiate as child 1 Answer

destroying an object instantiated that came out of spawn point 3 Answers

Instantiating and destroying object 1 Answer

Instantiate prefabs just before it comes into view 2 Answers

Instantiate JS error that i can't figure out 1 Answer


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