i have a list of about 150 enemies and am trying to have them check 3 exit boxes which destroy their instances. it takes me 3 foreach loops to see if they are touching the boundaries on the left right and bottom of the screen, there is also another foreach loop also on the same list checking if theyre touching the player.
is this ‘expensive’?
Probably not, but why not test it? Put your for loops inside another loop and run them multiple times. Increase the number of times you run the four tests until you start to see a FPS drop. Be sure to run the (final) tests in a build, not the editor. Note regardless of how big or small the impact, you can improve the performance by having your arrays be of transforms rather than game objects. There is a hidden GetComponent() call going from GameObject to Transform.
I presume your exit box is a trigger? If so why not just add a script on the enemy to check if they exit the boundary? http://docs.unity3d.com/ScriptReference/Collider.OnTriggerExit.html @roburtbu, That's some nice insight. I honestly had no clue that the GameObject didn't have a cached reference to the Transform.
As a heads up, a for loop is about twice as fast as a foreach loop. Furthermore, because of the version of Mono that Unity uses, there are some memory issues that come with using enumerators. I would avoid using foreach loops as a matter of principle.
@robertbu you know today I was actually starting to suspect there may be a hidden GetComponent() call. Is this also true of Component.transform, Component.rigidbody, etc? Is it worth writing my own getters with a caching mechanism?
You should definitely cache the transform and rigidbody, yes, as gameObject.transform functions pretty much as a getter that calls GetComponent Just storing a private variable localTransform or something on Awake is going to do the trick for you.
To answer your question - you’re doing 150*4 checks for if stuff is intersecting with some boxes and the players. That’s not a lot of calls in itself - so the efficiency completely depends on what you’re doing in those checks. If you’re being smart and finding the bounds of the exit boxes outside the loops instead of inside them, you should be good.
If you want to know more about how to reason about how much time an algorithm is going to take, you should read up on Big-O notation. Links: 12
The big question, though, is why you’re not using colliders to check if your enemies have entered the exit boxes, as this is exactly the kind of problem colliders are meant for. Do you have some kind of very special case check?
Probably not, but why not test it? Put your for loops inside another loop and run them multiple times. Increase the number of times you run the four tests until you start to see a FPS drop. Be sure to run the (final) tests in a build, not the editor. Note regardless of how big or small the impact, you can improve the performance by having your arrays be of transforms rather than game objects. There is a hidden GetComponent() call going from GameObject to Transform.
– robertbuI presume your exit box is a trigger? If so why not just add a script on the enemy to check if they exit the boundary? http://docs.unity3d.com/ScriptReference/Collider.OnTriggerExit.html @roburtbu, That's some nice insight. I honestly had no clue that the GameObject didn't have a cached reference to the Transform.
– ZephrimAs a heads up, a
– VesuvianPrimeforloop is about twice as fast as aforeachloop. Furthermore, because of the version of Mono that Unity uses, there are some memory issues that come with using enumerators. I would avoid usingforeachloops as a matter of principle.@robertbu you know today I was actually starting to suspect there may be a hidden GetComponent() call. Is this also true of Component.transform, Component.rigidbody, etc? Is it worth writing my own getters with a caching mechanism?
– VesuvianPrimeYou should definitely cache the transform and rigidbody, yes, as gameObject.transform functions pretty much as a getter that calls GetComponent Just storing a private variable localTransform or something on Awake is going to do the trick for you.
– Baste