Store a GameObject to a variable on runtime.

Hello there.

I am currently in the process of writing an .obj importer with support for models that have multiple objects, for usage in runtime. Parsing the object works fine, and for convenience purposes, a parent is created for each model with the separate objects being stored as its children.

alt text

Since i am parsing all of the .obj files when the game is started, i don’t want to use the meshes the moment they are successfully parsed, but instead i want to save them as a gameobject and then instantiate them whenever i need them.

So the logical thing to do would be (pseudocode):

var importedMesh:GameObject;
Instantiate parent;
Instantiate all objects for the imported mesh;
Set the parent for the instantiated objects to be the instantiated parent;
importedMesh=instantiated parent;

Which theoretically would allow me to then Instantiate importedMesh to create a new clone of the generated object. The problem is that importedMesh=instantiated parent; stores the instantiated parent as a reference, which means that if i Destroy the instantiated parent, importedMesh will point to nothing. That is certainly a problem since, as i have already mentioned, i don’t want to have the imported objects in the scene just yet.

So, is there a way to store a GameObject to a variable instead of storing a reference to it?

Edit: The point of this is to conveniently organize the imported models instead of storing meshes and having to instantiate each model’s objects every time i need them. Everything would be much easier if i were able to instantiate them once, give them a parent and then only worry about instantiating the parent. I already know the former is a valid option.

Simply? No, because that’s not how OOP languages work. What you have to do instead is explicitly copy the parent when you instantiate the new object. This means you just need another Instantiate call, with the source as the parent.

If you’re asking how to do this without copying, then what you’re asking is logically impossible.