Performance and memory

I have a bunch of creeps on a map. I need to save their location in a list when they enter a trigger and remove them when they leave. I also have to sort that list by their ID.

My question here is Is it cheaper performance wise to have 2 lists one holding the transform and the other it’s component? Or would it make a difference accessing each component to each transform? (this will be called often.)

I assume having the 2 lists is just a slight increase in memory but a huge savior on performance since it is not required to get the component? I am also assuming getting the component is done with reflection?)

1 Answer

1

Assuming you have some custom behavior (aka a component script) that represents the creep, then you can have an array of that component. But each component knows what GameObject it is from and what Transform component it has, with the .gameObject and .transform properties.

These are merely substitutes for GetComponent() calls, so it’s faster if you store (aka cache) the transform in the transform if you are going access it that way a lot.

lol I'm an idiot... I don't know why I was trying to go one script farther. lol I was getting the ID from a stats script which does not inherit mono. Completely forgetting about my creepController script that uses the creepStats. Thanks for pointing that out.