• 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 mcsweden1 · Mar 12, 2014 at 04:10 PM · javascriptprefabdestroyrigidbody2d

Rigidbody2D getting destroyed in the inspector

Hello, i have struggled with this issue for about 2 days now and cant find anything on the internet about it.

This is all the parts of the script

 //This variable is located in the player script
 var dagger_pref : Rigidbody2D;
     
     
 //This function is located in the dagger script
     function OnCollisionEnter2D(col : Collision2D)
     {
         
         
         if(col.gameObject.tag == "Block")
         {
             Destroy(gameObject);
         }
     
         if(col.gameObject.tag == "Enemy")
         {
             Destroy(col.gameObject);
             Destroy(gameObject);
         }
         
     }

  

The problem is that when i call for the object to be destroyed in the game, it also destroys the prefab that is attached to the playerscript which disables the player from using the daggers at all. I have tried with Destroy(this.gameObject) and Destroy(this.rigidbody2D), but they both yielded the same results. If anyone knows how to make the prefab stay in the inspector after it has been destroyed in the game i would appreciate an answer very much.

Thanks in advance.

Comment
Add comment · Show 8
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 huntsfromshadow · Mar 12, 2014 at 05:29 PM 0
Share

Where is that script located?

avatar image mcsweden1 · Mar 12, 2014 at 06:34 PM 0
Share

The dagger_pref variable is located in the player script so its not in the same script as the rest of the code. The rest of the code is in a script called dagger script that is attached to the dagger prefabs.

avatar image mrpalash · Mar 12, 2014 at 06:53 PM 0
Share

The object which you want to destroy, how that comes in scene? By Instantiate?

avatar image mcsweden1 · Mar 12, 2014 at 07:19 PM 0
Share

Yep, its instantiated from the player script

avatar image mcsweden1 · Mar 12, 2014 at 07:20 PM 0
Share

The object does get destroyed, but the prefab that i have attached to a Rigidbody2D in the player script gets destroyed/deleted as well. I only want the object that actually is in the game to go away.

Show more comments

2 Replies

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

Answer by huntsfromshadow · Mar 12, 2014 at 08:16 PM

Okay now this makes sense.

 dagger_pref = Instantiate(dagger_pref, transform.position, transform.rotation);

You are overwriting what is in dagger_pref with the result of the Insantiate. So when you destroy the object it will of course make a null.

If you want to keep the prefab around you need to make another variable.

 var daggerPrefab;
 var myInstance;
 
 myInstance = Instantiage(daggerPrefab....)
Comment
Add comment · Show 4 · 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 mcsweden1 · Mar 12, 2014 at 08:25 PM 0
Share

Hmm okay i understand the overwrite part. And am i supposed to make the myInstance variable a gameObject or a transform? Or just leave it like you presented it?

avatar image huntsfromshadow · Mar 12, 2014 at 08:27 PM 0
Share

Instantiate will return a gameobject so you want it to be that.

avatar image mcsweden1 · Mar 12, 2014 at 08:37 PM 0
Share

Okay, so a bit of fiddling with the code and a few playtests later its solved! Thank you so much for the help, i can finally continue working. I appreciate it very much. And thanks to everyone else who showed interest!

avatar image huntsfromshadow · Mar 12, 2014 at 09:45 PM 0
Share

$$anonymous$$ake sure you pick an answer so the system knows this has been answered.

avatar image
1

Answer by mrpalash · Mar 12, 2014 at 07:39 PM

Use this line instead of your Destroy function:-

Destroy(GameObject.FindWithTag("gameobject"));

Replace gameobject if your tagged name is different. Hope this makes your work... :)

Comment
Add comment · Show 1 · 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 mcsweden1 · Mar 12, 2014 at 07:47 PM 0
Share

Same outcome unfortunately. Im trying to figure out if i messed up big somewhere, but cant figure anything out. $$anonymous$$ight have to research for some other way to fire projectiles. Thanks anyways!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Instantiate prefabs just before it comes into view 2 Answers

Instantiate prefabs before it comes into view 0 Answers

Destroy a GameObject 2 Answers

Destroy Prefab using Javascript 2 Answers

Check if child exists and instantiate as child 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