Skip to main content
added 1 characters in body
Source Link
Tetrad
  • 30.1k
  • 12
  • 96
  • 143

Unity best practices are to never use Find in any of its incarnations at run time. It's slow and unnecessarily ties the name of the object with its functionality.

Any time you call Instantiate you can save off the return value and store it somewhere (i.e. a List in some component your write). Then you can just iterate over the elements of that list to get what you want.

So for your spawner example, your spawner could have a list of game objects that it has spawned. In Update you can iterate over that list and count the number of non-null gameobjects (to check for destroyed objects). Alternatively the enemy can have a reference to its spawner and remove itself from that list in OnDestroy. Then if that number is less than a certain value spawn a new one.

Unity best practices are to never use Find in any of its incarnations at run time. It's slow and unnecessarily ties the name of the object with its functionality.

Any time you call Instantiate you can save off the return value and store it somewhere (i.e. a List in some component your write. Then you can just iterate over the elements of that list to get what you want.

So for your spawner example, your spawner could have a list of game objects that it has spawned. In Update you can iterate over that list and count the number of non-null gameobjects (to check for destroyed objects). Alternatively the enemy can have a reference to its spawner and remove itself from that list in OnDestroy. Then if that number is less than a certain value spawn a new one.

Unity best practices are to never use Find in any of its incarnations at run time. It's slow and unnecessarily ties the name of the object with its functionality.

Any time you call Instantiate you can save off the return value and store it somewhere (i.e. a List in some component your write). Then you can just iterate over the elements of that list to get what you want.

So for your spawner example, your spawner could have a list of game objects that it has spawned. In Update you can iterate over that list and count the number of non-null gameobjects (to check for destroyed objects). Alternatively the enemy can have a reference to its spawner and remove itself from that list in OnDestroy. Then if that number is less than a certain value spawn a new one.

Source Link
Tetrad
  • 30.1k
  • 12
  • 96
  • 143

Unity best practices are to never use Find in any of its incarnations at run time. It's slow and unnecessarily ties the name of the object with its functionality.

Any time you call Instantiate you can save off the return value and store it somewhere (i.e. a List in some component your write. Then you can just iterate over the elements of that list to get what you want.

So for your spawner example, your spawner could have a list of game objects that it has spawned. In Update you can iterate over that list and count the number of non-null gameobjects (to check for destroyed objects). Alternatively the enemy can have a reference to its spawner and remove itself from that list in OnDestroy. Then if that number is less than a certain value spawn a new one.