Created
January 2, 2017 18:28
-
-
Save zapu/540a0f9c0082bc3cd5127c608a5b39e0 to your computer and use it in GitHub Desktop.
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
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