Skip to content

Instantly share code, notes, and snippets.

@zapu
Created January 2, 2017 18:28
Show Gist options
  • Save zapu/540a0f9c0082bc3cd5127c608a5b39e0 to your computer and use it in GitHub Desktop.
Save zapu/540a0f9c0082bc3cd5127c608a5b39e0 to your computer and use it in GitHub Desktop.
public static class MonoBehaviourEx
{
public static bool FetchComponent<T>(this MonoBehaviour beh, out T obj) {
obj = beh.GetComponent<T>();
return obj != null;
}
}
// example:
public class Hero : MonoBehaviour
{
Controller2D controller2D = null;
void Start() {
this.FetchComponent(out controller2D);
// instead of:
// controller2D = GetComponent<Controller2D>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment