I want to make a clone of a prefab in code, but only in looks- no logic. I’m attempting to make a very generic class that will do this, but am having trouble copying a component from an existing object to my new object.
I’ve read in Object.Instantiate that one can instantiate components, but I’ve also read in GameObject that ‘You can’t create Component and then make a reference from object to it.’ Now, putting both articles together, it looks like I can indeed instantiate a clone of a GameObject’s component, but I’m unable to actually add it to another GameObject… which makes me wonder just what use this is.
I’ve tried doing this 20 different ways, but so far, nothing has worked. Here is my last failed attempt, although it’s possible I’ve modified it so much it’s junk now.
Mesh FooMesh = Instantiate(this.gameObject.GetComponent<Mesh>) as Mesh;
foo.AddComponent(FooMesh);
Am I overlooking something obvious here? I don’t really want to modify all my prefabs and write junk code to add back all the various script components each time I instantiate one. Thanks in advance.