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
1
Question by $$anonymous$$ · Sep 17, 2014 at 02:39 AM · destroycomponentsthis

Remove "this" Component

If I add a Component to a GameObject using gameObject.addComponent();, and I want to remove that component from the MyComponent C# file, is there anyway I can do something like:

 Destroy(this);

instead of:

 Destroy(gameObject.GetComponent<MyComponent>();

I know the latter works, but it just bugs me that I have to specify the name of the component that I am in.

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

1 Reply

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

Answer by Bunny83 · Sep 17, 2014 at 02:45 AM

Of course you can use

 Destroy(this);

Keep in mind that this will only destroy the component but keep the GameObject and all other components. The Destroy method works with all instance-references that are derived from UnityEngine.Object. This includes GameObjects, Components, Meshes, Materials, ...

Unfortunately they removed the hierarchy view from the documentation. With it it was easy to see all classes derived from a certain base class like Object, Collider, Renderer, ...

edit
Actually your first version is the more reliable one since if the GameObject holds multiple instances of the same component, only the first one is returned and that isn't necessarily the instance invoking GetComponent.

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 $$anonymous$$ · Sep 17, 2014 at 02:48 AM 0
Share

Well I feel foolish. I thought Destroy(this) would destroy the actual script. Thanks!

avatar image Bunny83 · Sep 17, 2014 at 02:52 AM 0
Share

@$$anonymous$$: Uhm with "actual script" you mean the class itself? That's not possible since all classes are compiled into one (a few) DLL assembly files which can't be altered at runtime.

Destroy(this) will remove the script instances that is invoking that line.

avatar image Kiwasi · Sep 17, 2014 at 02:52 AM 0
Share

It destroys the instance of the script. Not the actual script itself.

avatar image $$anonymous$$ · Sep 17, 2014 at 02:54 AM 0
Share

I got scared cuz I accidentally deleted a prefab once. Thanks for your help!!

avatar image Kiwasi · Sep 17, 2014 at 02:58 AM 0
Share

Destroy shouldn't be able to touch assets. DestroyImmediate can.

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

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

Related Questions

How to safely destroy component with dependants? 2 Answers

Why does my bullet prefab get destroyed? 1 Answer

Destroy all scripts on object. 3 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

OnTriggerEnter - destroy (this.gameobject) if it collides with anything 2 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