- Home /
How do I extract all the components of a gameobject and place them in to a new gameobject?
Hey there, I'm trying to extract ALL the components of a gameobject and then place them in a new gameobject at a later date after the original gameobject has been destroyed.
In effect I'm trying to clone the gameobject without the original object being there.
In C#.
It's for an inventory script. The user approaches the object then presses 'E' to 'collect' it. The object is detected with capsuletracing. I know this part works.
This is what I've got so far:
First, the struct, which I create outside of the main class:
public struct InventoryItems { public string itemName; public Component[] itemComponents; }
Then start:
void Start() { realInventory = new InventoryItems[10]; inventoryCollectionPrompt=0; }
Then inside update:
if(Input.GetKeyUp(KeyCode.E)) { realInventory[0].itemName = hit_fwd.transform.gameObject.name; realInventory[0].itemComponents = hit_fwd.transform.gameObject.GetComponents(typeof(Component)); Destroy(hit_fwd.transform.gameObject); }
I know it gets the right number of components (in this case 5) through Debug.Log(realInventory.itemComponents.Length).
Then I try to 'restore' the gameobject by pressing 'F':
if(realInventory[0].itemName!=null) { if(Input.GetKeyUp(KeyCode.F)) { restoreItem(); }
This is the restoreItem procedure:
void restoreItem() { int i=0; GameObject Cake = new GameObject(realInventory[0].itemName); Cake.transform.position = transform.position; while(i < realInventory[0].itemComponents.Length) { Cake = Cake.AddComponent ("Test"); i++; } }
For some reason, AddComponent only accepts a string rather than component. Same with GetComponents. Where I have used it is simply guesswork, as I have never used AddComponent before as per the Unity script reference which is remarkably non-descriptive in this case.
I have tried to be as detailed as possible in my explanation of what I am trying to do. If I have left something vital out, please ask for it.
I would also like to request that answers are more than code correction/replacement and actually include explanations as to why those corrections/replacements are there, as it helps to further my learning.
Thanks in advance.
As far as I know, and I may be wrong since I've never used AddComponent before either, AddComponent adds a component with all of its inherent values set to default. The reason it requires a string is because it adds a component with the name of the string value you passed. It only needs that name to add the component because all other values of the component will be set to their default values anyway. Logically, it makes sense, but since I haven't tested it myself, I can't definitively prove it. Anyway, try the following line of code:
Cake.AddComponent<(typeof(realInventory[0].itemComponents[i]))>();
(Your code is trying to replace the value of the game object Cake with the value of the component you are adding.)
I can't assure you that it will work though, since like I said, I've never used AddComponent before. If it doesn't work, try casting it as a string. But remember that if it does work, the values in your component will most prolly have been reset to their default values.
Do the values of the components in your item change in the first place? If they don't, you can just make a prefab out of your item and instantiate that ins$$anonymous$$d.
As an aside, you might want to use a for loop ins$$anonymous$$d of a while loop in your restoreItem method.
Answer by Wolfram · May 28, 2011 at 10:39 PM
Instead of destroying a GameObject and recreating it identically some place else, just disable it temporarily, for example with gameObject.SetActiveRecursively(false).
If you want to create a clone of a GameObject, just use newObject=Instantiate(originalObject). You cannot copy or move individual Components this way, it's all or nothing.
If you want to move an object to a different point in your scene hierarchy, just reparent it with myObject.transform.parent=newParent.transform
You cannot copy individual Components and keep their settings.
This is great, it works perfectly. I understand your answer. I cannot copy the gameobject, but I can disable it (so for all intents and purposes it is removed from the game world). Then I can move it and then reactivate it as I choose, whilst at the same time making it appear to have been stored as an inventory item using various other means. Thanks! :D