Last active
August 29, 2015 14:07
-
-
Save Naxum/803d8f7b55397d672385 to your computer and use it in GitHub Desktop.
Quick Instantiate with returned component of prefab you used to spawn it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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