Skip to content

Instantly share code, notes, and snippets.

@pr00thmatic
Last active June 17, 2019 18:51
Show Gist options
  • Save pr00thmatic/23a22f6b26a157b994e7bde11e1abfb0 to your computer and use it in GitHub Desktop.
Save pr00thmatic/23a22f6b26a157b994e7bde11e1abfb0 to your computer and use it in GitHub Desktop.
public static GameObject SafePrefabInstantiate(GameObject reference){
return SafePrefabInstantiate(reference,Vector3.zero,Quaternion.identity);
}
public static GameObject SafePrefabInstantiate (GameObject reference,
Vector3 position,
Quaternion rotation) {
#if UNITY_EDITOR
if (Application.isPlaying) {
return GameObject.Instantiate(reference, position, rotation);
} else {
GameObject obj = PrefabUtility.InstantiatePrefab(reference) as GameObject;
obj.transform.position = position;
obj.transform.rotation = rotation;
return obj;
}
#else
return Instantiate(reference);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment