Summary
The default cloning method which is called when I call Instantiate(obj) is not working as I need it to for my script. Does this Instantiate() call, call a member function on obj to do the copying? If so, can I write my own custom version?
If not, how does it work?
Details
Here is a simplified description of my problem:
I have a GameObject called sampleGO. This has a script attached called SampleSc - which looks something like this:
class SampleSc : MonoBehaviour{
public GameObject meshPrefab;
GameObject displayedMesh;
void Awake(){
displayMesh = Instantiate(meshPrefab) as GameObject;
displayMesh.transform.parent = transform;
}
};
Now, suppose elsewhere in my program I attempt to clone my sampleGO. E.g.
GameObject newGO = Instantiate(sampleGO);
Then I get a problem, because while in the original, the child of the sampleGO and the displayMesh in SampleSc point to the same object - in my cloned version, they point to two new objects (i.e. both displayedMesh AND the transform’s child have been cloned - even through originally they were a single object).
While I am sure I could work around this by (e.g. not having the displayedMesh variable in the class), I would like to be able to write a custom clone/copy/whatever function to override the default behaviour (there are other reasons why the default behaviour is not good for me too - which I have not gone into here).
Please help!
Diarmid