/// <summary> /// Serializes the source GameObject to an XmlDocument and /// then deserializes that document to create an (hopefully) /// exact clone of the source object. /// </summary> /// <param name="source"></param> /// <returns></returns> public T CloneComponentWithDeserializer <T>(T source) where T : Component { T dest = null; XmlDocument doc = null; try { doc = XmlSerializer.Serialize(source, 1, "Component"); } catch (Exception e) { Assert.Fail("There was an exception in the serialization process.\n'" + e.Message + "'"); } GameObject go2 = new GameObject(); try { dest = XmlDeserializer.DeserializeComponent <T>(doc.InnerXml, 1, go2); } catch (Exception e) { Assert.Fail("There was an exception in the deserialization process.\n" + e.Message + "\n--------------------\n" + e.StackTrace); } if (go2 != null) { Undo.RegisterCreatedObjectUndo(go2, "Deserialized test GameObject"); } else { Assert.Fail("Failed to deserialize back into a GameObject."); } return(dest); }