- Home /
How to apply script saved in variable
Hi to all. I want to add a component saved to a variable from "heal heart" to a character, who will collide with it. I dont want to use col.gameObject.AddComponent (); Because there will be several different effects, and I want only to change effect. I tried something like col.gameObject.AddComponent (); col.gameObject.AddComponent(typeof(testHeal)); This is all wrong.
If there is other, more correct way, tell me pleae, thank you!
Answer by Laurentino · Mar 07, 2018 at 09:19 AM
If I understand correctly, you want to move a component from a GameObject to another. This can't be done, you would have to add a new component and copy the values.
There is probably an easier way to achieve what you're attempting to do.
Thank you for you answer Laurentino! I did it in that way, as you have said. But I should write down the type of class in script every time, with every new type of effect, in this way, its ok, but I wanted to find more effective way.
Answer by meat5000 · Mar 07, 2018 at 09:22 AM
$$anonymous$$ost welcome. Interfaces are used in cases where you want to able to define something that can be used in any object. Classic examples are making things I$$anonymous$$illable, IReloadable etc. I admit, I do struggle with the concept a little as when I first discovered them I expected them to do something more along the lines of Polymorphism so my brain shut them out for a while. I think the advantage comes in the ability to skip defining of types but I'm fairly new to them myself.
Your answer
![](https://koobas.hobune.stream/wayback/20220612154047im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Best way to get variable from another script 1 Answer
Array of Objects of a Class 1 Answer
Component Class Hierarchy 2 Answers
PropertyDrawer dynamic subdrawer 0 Answers
Adding to a List and putting it through a function? 2 Answers