- Home /
Copying GameObjects with reference types
I have a MonoBehaviour, which contains nested lists of references to classes. My serialization works fine, but when i copy my GameObject the references in those lists now points to same objects. I would like to create deep copies of those objects.
I could do this manually by using editor scripts, but i would like to have it working when manually copying gameobject inside Unity.
How does Unity duplicate lists? Can i somehow take part in this process?
"but i would like to have it working when manually copying gameobject inside Unity"
You mean with the copy / paste / duplicate editor commands? Huh. Dunno.
Even if it's possible, something tells me you'd be better off making your own specialized method versus overriding what Unity does natively. Could even maybe bind a shortcut key to your special deep duplication method.
Answer by mconradie · Jul 19, 2015 at 07:03 PM
Assuming the items in the list are clone-able you could create an extension like:
You can use an extension method.
static class Extensions
{
public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable
{
return listToClone.Select(item => (T)item.Clone()).ToList();
}
}
Answer from here
Your answer
Follow this Question
Related Questions
Reference an Instance of an Object?C# 1 Answer
How to make changes to a copied object 1 Answer
How do you reference different cameras 2 Answers
gameObject are not referenced 2 Answers
How to get a referance to an instantiated prefab clone ? 1 Answer