Skip to content

Instantly share code, notes, and snippets.

@johnsoncodehk
Last active March 24, 2017 06:55
Show Gist options
  • Save johnsoncodehk/6da46453ce2f870b6f87f431bb267b9c to your computer and use it in GitHub Desktop.
Save johnsoncodehk/6da46453ce2f870b6f87f431bb267b9c to your computer and use it in GitHub Desktop.
Unity GameObject Clone Extension
/*
* https://gist.github.com/johnsoncodehk/6da46453ce2f870b6f87f431bb267b9c
*/
using UnityEngine;
public static class CloneExtension {
public static GameObject Clone (this GameObject gameObject) {
GameObject newGameObject = Object.Instantiate (gameObject);
newGameObject.transform.SetParent (gameObject.transform.parent, false);
newGameObject.SetActive (true);
return newGameObject;
}
public static T Clone<T> (this T component) where T : Component {
T newComponent = Object.Instantiate (component);
newComponent.transform.SetParent (component.transform.parent, false);
newComponent.gameObject.SetActive (true);
return newComponent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment