- Home /
Instantiate an instance (clone) only copies public fields?
I cloned an instance by using Instantiate() and passing the instance, not a prefab, but an instance already in the scene. All public fields were copied but all private or protected fields were reduced to the type default.
I expected an exact copy.
Is this result the expected result or a bug?
I guess I can pass a reference to the original and then reference that, but that is a hack that would only work in rare situations.
Answer by Eric5h5 · Aug 19, 2011 at 04:27 AM
As far as I know, a deep copy requires [Serializable], which doesn't do anything for private variables, so what you describe is what I would expect.
It looks like these work :
public Transform ref;
[HideInInspector]
public Transform ref;
Obviously only the first shows up in the inspector, which includes the debug tab.
I'm not sure if this will matter, but in theory anything that shows up in the Inspector's debug tab should work and I just made a property which, to my surprise, shows up:
public $$anonymous$$yType myType { get; private set; }
shows up as
k__Backing Field
...with the reference after as you'd expect. I'm guessing this means that anything with a public getter would work? (Untested)
Your answer
Follow this Question
Related Questions
Getting a copy/clone of an object from an array (C#) 1 Answer
How do I properly destroy an instance of a prefab from within its own script? 1 Answer
Variable value persists when public but is lost between frames when private 1 Answer
How to copy the real size of another sphere gameobject 2 Answers
How can I duplicate an object and place it randomly near the original ? 0 Answers