Skip to content

Instantly share code, notes, and snippets.

@Naxum
Last active August 29, 2015 14:07
Show Gist options
  • Save Naxum/803d8f7b55397d672385 to your computer and use it in GitHub Desktop.
Save Naxum/803d8f7b55397d672385 to your computer and use it in GitHub Desktop.
Quick Instantiate with returned component of prefab you used to spawn it
using UnityEngine;
/*
I really hate having to type this long line of a code to get the component of a prefab I want to spawn (Transform for simplicity)
Transform t = (GameObject.Instantiate(prefabTransform.gameObject) as GameObject).GetComponent<Transform>();
Now all I have to type is:
Transform t = NaxHelper.Spawn(prefabTransform);
Feels good, man.
Change up this script however you want, just keep making games. :D
*/
public class NaxHelper
{
public static T Spawn<T>(T obj) where T : Component
{
return (GameObject.Instantiate(obj.gameObject) as GameObject).GetComponent<T>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment