For some reason I can’t post questions on the answers section, so I’ll try here.
I’m trying to copy a component that has a reference to a Prefab.
When copying that component, it actually stores a reference to the reference for the prefab.
So when I destroy the original component, the prefab reference is gone from the copied component.
Is there a way around this?
My Copy Component method goes like this:
public static Component CopyComponent(Component original, GameObject destination)
{
System.Type type = original.GetType();
Component copy = destination.AddComponent (type);
// Copied fields can be restricted with BindingFlags
System.Reflection.FieldInfo[] fields = type.GetFields();
foreach (System.Reflection.FieldInfo field in fields)
{
if (field.GetType() == typeof(GameObject)) {
field.SetValue (copy, field.GetValue(original));
} else {
field.SetValue(copy, field.GetValue(original));
}
}
return copy;
}