Advertisement
Pro_Unit

CopyComponent Extension Method

May 15th, 2022
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.41 KB | None | 0 0
  1. public static T CopyComponent<T>(this T original, GameObject destination) where T : Component
  2. {
  3.     Type type = original.GetType();
  4.            
  5.     Component copy = destination.AddComponent(type);
  6.            
  7.     var fields = type.GetFields();
  8.            
  9.     foreach (System.Reflection.FieldInfo field in fields)
  10.         field.SetValue(copy, field.GetValue(original));
  11.            
  12.     return copy as T;
  13. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement