Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by grimofdoom · Mar 22, 2016 at 06:21 AM · c#arraylists

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.

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Mar 22, 2016 at 10:30 AM 0
Share

Do the transforms have colliders?

avatar image grimofdoom · Mar 22, 2016 at 04:23 PM 0
Share

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)

avatar image TreyH grimofdoom · Mar 22, 2016 at 04:42 PM 1
Share

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.

avatar image Salmjak · Mar 22, 2016 at 04:55 PM 1
Share

@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).

You might also want to read this.

avatar image grimofdoom · Mar 22, 2016 at 04:56 PM 0
Share

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)

avatar image TreyH grimofdoom · Mar 22, 2016 at 05:00 PM 0
Share

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.

avatar image Salmjak grimofdoom · Mar 22, 2016 at 05:08 PM 1
Share

@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).

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Compare GameObject with an array 1 Answer

How to spawn sprites in a row randomly from a single array without double-ups 0 Answers

Using foreach to remove and delete bullet in List - C# 3 Answers

Array with pushing values? 0 Answers

Index out of range error 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges