- Home /
Can I move a component from one GameObject to another in script?
I want to swap a gameobject out for another one in script and move the itween component from the first gameobject to second so it continues along the smooth path that the first one had started. Is this possible?
Just to clarify - I need to know how to do this, not just if it's possible. Sorry for the confusion.
Answer by syclamoth · Sep 15, 2011 at 01:29 AM
Moving components around is a tricky thing to want to do- often components can have complex dependencies which make it difficult to be sure they will still work after cloning. What exactly do you mean by "swap a gameObject out"? If you only want to change what it looks like, you can swap out the mesh and texture data much more easily than moving around whole components, since these are just parameters of common components. If I were you, I would look at what it is exactly that you want to do, and try to do that instead. What are the important differences between your original object and the one it is being replaced by? Is there anything large enough to actually justify changing the entire gameObject?
EDIT- To clarify, to do what you want to do, you need to put the iTween component on to a parent object which moves around, and make the replaced GameObject a transform child of that object, so that it can be swapped out. This way, the iTween component is not affected by the swap, and you still get to have a completely different GameObject afterwards.
I am sure it will work after I clone it. Yes, it's important that I replace the object completely, and not just a part of the object.
What exactly does your gameobject do? What are the changes you will be making? While it is very difficult to just swap a component out the way you want to, I still think it should be possible to make something work the way you want it to- I just need to know what, exactly, you need to do! Is the reason you can't just swap mesh data because you need different scripts on each GameObject, and those don't come easily? At this point, I would make the gameObject you want to swap around be a referenced variable on the swapper script, and put the functionality from the iTween component inside of a controller object which watches from above so that it doesn't get destroyed when you make the swap.