- Home /
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
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
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! ;)
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.
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.
Your answer

Follow this Question
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