Instantiating runtime-built object produces duplicate children

Hello,

I’ve got a weird problem here… let me try to explain the setup:

I’ve got a script here, that will import a model in runtime, and, through a config file, will add components and set a few values, based on the script. So far so good.

The thing is, once loaded, I’m not ready to have the imported objects on stage yet… so I parented them to the loader object (the one running the import script), and disabled them, so they’re there, but invisible.

Then, when I want a new object on scene, I’m calling Instantiate on this disabled, “source” object, as if it were a prefab. (In fact, this whole setup is to emulate prefab instantiation, only at runtime)

The problem, is that, for some reason, the instantiated copy comes with all it’s children in duplicate…

It’s very weird… I’m only calling Instantiate once, and even if that were the case, it would have produced 2 copies, instead of a single one with duplicate children…

Here’s a hypothesis:

The imported object isn’t stored just as it came in from the importer… First, I rename the imported object to “model”, and parent it to a new, empty gameObject I’ve created. This empty gameObject is the actual root of the object, and it holds the master component for it, and has the model, some particle effects, and another object that holds a meshcollider as it’s children. So, the imported model is only a child of a bigger object I’m constructing.

The object hierarchy tree then, looks like this:

-MyObject
    -model
         -modelChildObject
         -anotherModelObject
    -collider
    -particle effects

Now, when I call Instantiate, what I get is this:

-MyObject
    -model
         -modelChildObject
         -modelChildObject
         -anotherModelObject
         -anotherModelObject
    -collider
    -particle effects

So, it seems that somehow the imported model is taking it upon itself to re-instantiate it’s children, but what happens is that having already been instantiated, this results in duplicate children…

So, why could this be happening? Somehow the Instantiate thing is propagating itself beyond what is needed of it…

I realize it might be very hard to understand what’s happening here, since it’s quite a complex setup… My game is getting past that point where one can ask for help, since most problems are now dependent on it’s own logic…
But please, if you have any thoughts about this, do share your ideas :wink:

As always, thanks in advance!!

Cheers

Ok, to follow up:

Further testing has showed that the problem is just the imported model here…
Calling instantiate on it will for some unaccountable reason duplicate all it’s children…
This means it has nothing to do with me parenting the model to a new gameObject… it’s something in the model itself…

This is very strange…

If it helps, the runtime import library I’m using is this one: jon-martin.com – Unity3D DAE, OBJ import at runtime 2010.
I’m using it to load in a .dae (COLLADA) model, exported from 3Ds Max…

It loads in fine… the imported model is perfectly structured… but Instantiating it will create a copy with duplicate children

Any ideas?

Cheers

Ok, I solved it!!

It was so weird, it couldn’t have been a bug gone unnoticed…
The importer creates some loader components which are discarded once the loading is done… The problem is that they are discarded by calling Destroy() on them, which will wait until the frame ends to safely remove the object from scene…

I was testing instantiation by having the just-loaded object instantiated right after processing, not realizing that the Destroy() call hadn’t taken effect yet… The loader component would get instantiated along and basically reload the model, while I, on my end, was copying the original parts in…

This resulted in duplicate children inside the loaded model!

Go figure!!

Cheers