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 T. · Jan 19, 2011 at 01:13 AM · javascriptcomponent

store a component in a variable?

hey is there any way to store a component in a variable?

i have a few scripts attached to a game object and would like to enable/disable. basically

activeComponent = whatever it is

then when i want to turn it off and turn on the new component

 disabledComponent = activeComponent
 activeComponent = whatever it is

is there anyway to do this?

also! is it possible that when I disable these scripts I can disable everything they have done? i.e. script one displays a 3D object, when I'm disabling it I want that object to disappear as well .etc

thanks

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Flynn · Jan 19, 2011 at 01:25 AM

You ask some interesting questions!

Component switching as you described is impossible, as components are tied to their respective gameobjects. You can disable one, but you can't have more than one, and actively disable and enable them in turn.

The closest you can get to this is to first save all relevant settings in your own custom class, then remove the component, and add a new one, and load relavent settings from a different variable using the same custom class you created.

As for disabling a script and undoing everything it did in the PAST, that is impossible. Creating a 3D object with a script sends it to Unity's memory tracking. Deleting the script's memory does not delete the object.

Again, about the best you can do is keep track of all relevant changes your script did, and then undo those changes manually.

Hope this helped -- Flynn

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 T. · Jan 19, 2011 at 11:39 PM 0
Share

thanks for that!

however, all my component scripts are attached to the same gameobject, does this change it in anyway?

what i am trying to do is store that component (a script) in the variable so that i know what has been called/what is about to be called and can call the off/on functions respectively.

now that i think about it, its probably easier to just come up with a bunch of names that reference the various scripts and use those rather than actual references to the components, but i like to make things hard for myself! ;)

avatar image Bunny83 · Jan 31, 2011 at 12:15 PM 0
Share

impossible? no, not at all - You can attach as many components as you want. Only the builtin special components can be attached only once except: Joints, AudioSources, NetworkViews and the Terrain script (but that makes no sense). All scripts can be attached multiple times to the same GO.

avatar image
0

Answer by Jesse Anders · Jan 19, 2011 at 01:25 AM

Yes, you can store a component (more specifically, a reference to a component) in a variable.

As for causing an object to disappear, that can be done by disabling the renderer associated with the object.

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

No one has followed this question yet.

Related Questions

How to disable/enable SSAOEffect or other specific Components? 1 Answer

Cannot reference "Halo" type in Javascript 2 Answers

How delete or remove a component of an GameObject? 4 Answers

Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer

Add a script to a gameobject that doesn't exist yet 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