Remove component instantly and add new one
Hello everybody. I've got a problem. I use a virtual BaseClass as base lass for few other classes. And, for example, I use ChildClassA and ChildClassB inheriting them from BaseClass. Then. On the BaseClass I use [DisallowMultiplyComponent] attribute to prevert multiply usage of ChildClassA or ChildClassB.
Next, I need to replace ChildClassA with ChildClassB at current gameObject at runtime. And I cant to find a way to do this. Destroy(GetComponent()) and then AddComponent() will not work because ChildClassA will be removed after Update() call will be finished. So, at runtime, AddComponent will return error because current GO already has BaseClass instance. Furthermore, it happent on animation event call, so I can't use DestroyImmediate. I can't to find a way to just remove component from gameobject without destroying it. And I can't replace it with new one.
So, is here are any way to do this?
I undestand, that I can remove [Disallow$$anonymous$$ultiplyComponent] and all right. But I don't want to do it. I'd like to keep this attrubute for my insureance, that I'll never add more then one component via inspector.
Also, sure I can add new component to any empty child... but it's not a good way too.
One idea. I can use Coroutine which will wait for next update and then add new component. Not a best solution, but it should work.
Your answer
Follow this Question
Related Questions
Create a clone without components at runtime 0 Answers
Does my script become stupid, or do I become stupid? 1 Answer
Components not recognized. 1 Answer
component greyed out 1 Answer
how to get component of array in unity 0 Answers