Skip to content

Instantly share code, notes, and snippets.

@hunterloftis
Created February 25, 2016 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hunterloftis/f76266f1b0c92bcfba4f to your computer and use it in GitHub Desktop.
Save hunterloftis/f76266f1b0c92bcfba4f to your computer and use it in GitHub Desktop.
// both the 'Player' component and this component exist as scripts on the player instance in the scene
// My guess is that start() is called on this component before on Player()
// (even though this script is listed last)
// Is this a bad pattern? It seems like running GetComponent() on each FixedUpdate() and checking for null would be a huge waste
// How do unity developers do this instead?
void start() {
player = GetComponent<Player>();
}
void FixedUpdate() {
player.DoSomething(); // NullReferenceException: Object reference not set to an instance of an object
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment