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


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

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.