- Home /
How to clone a gameobject in runtime (not with instantiating)
Hi,
I'm instantiating a game object and performing a boolean mesh operation on it (effectively making a hole in it) and saving it as a new game object (all in runtime).
How can I clone this new game object in runtime?
Thanks!
Perhaps I've misunderstood. "Not with instantiating" seemed like a nonsensical request, but maybe there's something to this I don't know about. What expectation do you have that instantiating doesn't satisfy? Publishing.
@vexe It does an awful good job of sounding like one, though!
What can I say? I'm lexicologically lackadaisical.
Create empty GameObject
Add: (mesh renderer, mesh filter)
Copy vertices of GameObject 1's mesh filtes to vertices of GameObject 2 meshfilter
Build mesh
But that's basically just Instantiating manually. Will be a lot more difficult to texture this properly I guess.
Answer by AlwaysSunny · May 01, 2015 at 01:21 AM
You can Instantiate() any game object to which you have a reference; it doesn't need to be a prefab. It typically should be a prefab, but in your case, I suppose not. Consider keeping a hidden "original" version to clone from on which you'll zero-out any transform changes so everything behaves nicely.
Is your boolean script super robust and performant? And did you make it yourself? Gimmie gimmie gimmie! ;)
That worked! Never knew you could instantiate non-prefabs, thanks for your help!
@AlwaysSunny I didn't know that! I'll try it later and post the results. The shape is made in runtime so there's no way I can save it as a prefab before starting the game, but I thought you could only instantiate pre-defined prefabs!
The script is called Boolean Ops on the asset store (u3d.as/content/alan-baylis/boolean-ops/80j), it works great normally but I modified the code to work in runtime and it's a bit slow when perfor$$anonymous$$g the operation on 6 pieces of geometry in one method. That's why I needed to copy them ins$$anonymous$$d of cutting the same shape three times (as there are 6,3 of one wall and 3 of another).
Thanks for your help, I'll post results later!