- Home /
How does Instantiate() clone an object
Hi,
I searched but couldn't find any details on how exactly Instantiate()
clones an object, specifically when that object has scripts attached to it, and those script have class variables.
So for example:
class test1 : MonoBehaviour {
public int num;
public List<int> multiNum;
public MyClass myObj;
}
now, if I instantiate an object with test1 script attached to it, will it clone the value of num
variable? What about multiNum
variable? And last, what about myObj
which is a class that I created. Will it clone it? If yes, then how does it clone it?
Thanks
This is just an example, I'd like to understand the underlying mechanics in general.
For instance, if the $$anonymous$$onoBehaviour had a Clone()
method I'd assume it's being used and I can override it, but it doesn't, so that's out of the question. Is there some other way that $$anonymous$$onoBehaviours are cloned? I can't figure that with trial and error.
Answer by whydoidoit · Oct 13, 2013 at 04:10 PM
It will make a copy of anything the serialization system can normally save so all basic types, all classes marker Serializable, lists and arrays. It will not copy anything else (like Dictionaries etc).
All object references to components within the tree defined as descendants of the object being cloned will be fixed up to point to the copied items.
Thanks. Any recommended links to read more details about the serialization system you mentioned?
Your answer
Follow this Question
Related Questions
Associate objects to a prefab 1 Answer
Duplicating a prefab 1 Answer
Classes, MonoBehaviours C# 2 Answers
Start being called after Update of another GameObject? 0 Answers
How do I get a CS script in assets to be a stored monobehaviour variable 1 Answer