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 Kelstek · Aug 15, 2014 at 04:38 AM · destroyrequirecomponent

Dynamically removing required components when destroying

Scenario:

  1. A GameObject has Component1, which requires, say, a box collider trigger.

  2. ScriptA adds Component2 -- which also requires a box collider trigger -- to the GameObject.

  3. ScriptB adds Component3 -- which also requires a box collider trigger -- to the GameObject.

  4. Later, ScriptC Destroys Component3.

In an interest in tidiness, ScriptC should destroy Component3's required box collider. B ut the box collider shares a codependency with Component1 and Component2.

I don't see anything in the RequiredComponent documentation that points to how I might check whether any other components also require the box collider. (i.e. programmatic access to RequiredComponent attributes of components).

Any ideas on how to best solve this problem? Would I have to eliminate the RequireComponent attribute and programmatically add required components in Start(), and track them via that method? I'm hoping I've failed on the research/Google front and there's a great way to do this via baked in methods.

Comment
Add comment · Show 2
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 kumarc123 · Aug 15, 2014 at 04:46 AM 0
Share

Why do you want to destroy the collider? If you don't want that collider which is required by others then don't refer to that collider in Script-C.

avatar image Kelstek · Aug 15, 2014 at 01:56 PM 0
Share

@kumarc123 - Take the simplest case - ScriptA adds a component that requires a box collider. ScriptB destroys the component. An orphaned box collider is left on the gameobject, which I'd like to prevent, as ScriptA and ScriptB might fire across many objects across a gameplay session, needlessly leaving many orphaned box colliders.

I suppose any other scripts requiring the box collider will prevent its destruction ("Can't remove BoxCollider because YourScript (Script) depends on it"), so I can have each script that destroys a component that requires the box collider to try/catch an attempt to destroy the box collider, too. That has its own pitfalls - what if the object naturally had its own box collider to start?

For the case where ScriptA adds the only component that requires it, how would one later tell what required components exist due to the component being destroyed without a custom framework built around it all?

Is there any way to introspect the RequiredComponent attributes of a Component to see what dependencies exist?

Or, would conventional Unity wisdom indicate that leaving "orphaned" box colliders that are triggers all over the place has negligible performance impact?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by smoggach · Aug 15, 2014 at 02:14 PM

Your best bet is to use something else to govern the collider. I'd suggest another little script who does nothing but figure out whether his object needs a collider or not.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Destroy parent of child gameobject? 3 Answers

The object of type 'Rigidbody' has been destroyed but you are still trying to access it. 2 Answers

Destroy and Instantiate 2 Answers

Object wont object get destroyed 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