Instantiate deep copy of component member's members

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?

I’m not sure if I understand exactly what you’re trying to do, but if you just need a basic deep copy of your object you can use a copy constructor:

 [System.Serializable]
 public class MyClass {

 public Vector3[] vectors;

 public MyClass() {
     vectors = new Vector3[2];        
 }

public MyClass(MyClass prevMyClass) {
    vectors = prevMyClass.vectors;
}

}

Just create an object with the object you want to copy like this: MyClass myNewClass = new MyClass(copiedClass);