I couldn’t quite find an answer tothis while searching, so here goes nothing. Consider the following:
public class MyComponent : Monobehaviour {
public MyClass myClass;
public Start() {
myClass = new myClass();
}
public Copy() {
myClass.vectors[0] = Vector3.one;
Instantiate(gameObject);
}
}
[System.Serializable]
public class MyClass() {
public Vector3[] vectors;
public MyClass() {
vectors = new Vector3[2];
}
}
If I call Copy(), a copy of the gameobejct is created, and I can see that the MyComponent script is exposing the MyClass object in the inspector, and that the Vector3 array is of the correct size. However, the Vectors are both zeroed out on the copy, while the original has the correct value of Vector3.one for the first array element.
I imagine this is happening because when the object is copied, it runs the constructor of MyClass, which zeros everything out. What I would like to know though, is if there is a way to let unity know to not only copy the class, but also its members?