least 'expensive' way to go through list and check transform.position
I am trying to find the least 'expensive' (quickest) to go through a list and check each item for its transform.position.y(specifically). There will be multiple (many) with the same Y value found, and I need all of them. For part of my game, I am working in voxel layers, and I want to set a setting to view only the specific layer the player is on, and the rest of the items NOT on the layer to not render(I know how to set that already). I will also at some point need to grab that items specific location in the list so I can remove it when the item gets deleted. I am currently using 'List' , and using a 'foreach' loop will be very expensive if/when there are hundreds of items in the list.
yes. the transforms in the list all have colliders currently. (I did think of adding a script to all of them to control the , but I would like to manually call the method to do it all at once ins$$anonymous$$d of having them constantly checking)
Since they have colliders, you could keep a massless, disabled box object lying around.
When you want to check things along a certain y-position, stretch the box nearly infinitely along the X and Z dimensions, set its y-position to your area of interest, then enable the box's collider.
Putting some little script onto that box with a List of transforms (which you would append with each OnCollisionEnter event) will give you the objects in question.
edit: it will need a rigidbody, but this could be a trigger event as well.
@grimofdoom Do NOT use foreach loops in Unity! Foreach is a garbage generator when used with Unitys standard implementation. Use for()-loops. Hundreds of items are nothing and should run in a for-loop in under 1 millisecond.
Also, do not compare Vector3's! Their Vector3.Equals-method instantiates a new Vector3 for every call to the method. I don't think this is called though since you compare floats. Check the profiler (do a deep profile).
how would I grow a cube 'infinitely' without it going in reverse (thinking you are talking about 'scale'). I understand the rest of it all , and have only a slight idea for turning render back on (OnCollisionExit or something)
Yeah, you'd just stretch the scale to your scene's extents. Your y-scale will be fairly small btw, to prevent accidentally scooping up something you don't really want.
I imagine your voxels have their own layer, so you might consider a special physics layer for this contacting box (so that it only hits the voxels) if you do go this route.
@grimofdoom You should really profile your code (use the profiler and do a deep profile). $$anonymous$$y recursive negamax function goes through 100,000 items in a couple of milliseconds. A few hundred shouldn't be a problem for a for-loop (neither would 10,000).