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 /
avatar image
0
Question by captPivoine · Oct 26, 2018 at 05:29 PM · array list

List or Array?

Hello.. I have that dilemma about using list or arrays.. Here s the context: I have those game objects randomly spawning at fixed location. I have that spawnSpots array populated with instances of a SpawnSpot prefab that are placed at different points of the map. Now, i want to prevent spawning on an already occupied SpawnSpot. My basic idea is to have SpawnSpot removing itself from the spawnSpots array when it is occupied (and re adding itself when it becomes available again). But apparently arrays contains a fixed number of elements, so it may not be easy to remove and add element to it and i should change my spawnSpot array for a list, which is supposed to be more flexible. But, also apparently, arrays are more efficient in terms of performences... So here is my question: - Should i go for a list instead of the array (according that there will never be more than, say, 16 SpawnSpots, maximum)? or stick to the array?

Alternatively, would it be possible to filter objects in the array, say by tag? (so i wouldnt have to bother about removing/adding the available objects from the array, but just tag them as "good for selection")?

And finally, please dont write me any code.. Just point me to the right direction :)

Cheers

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by hexagonius · Oct 26, 2018 at 08:19 PM

first of all, performance is not in the least a reason for choosing one over the other in your case. it's not a tight loop or anything. In your case it's just a matter of convenience.
you could have an array of bools in parallel you toggle. you could have a list, actually two to not forget choices... puck your favorite.

Comment
Add comment · Share
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
0

Answer by Will_Croxford · Oct 26, 2018 at 06:20 PM

Object pooling - https://www.raywenderlich.com/847-object-pooling-in-unity

See the code on this page, download his sample project. He uses a List, the key to performance is actually pre-insantiating all the objects, and not creating them and destroying them in the game. He does it in code, but you could just use the Inspector to create all the spawn points if they're in fixed positions.

you can index the objects in the array and pop and push like a stack (as in computing science definition of stack) from my memory, so may be no need to tag the objects. I think the idea is you have 16 spawn spots inactive at start of game, then activate them one by one, deactivate when you need, then if you need more than 16 at some point, you increase size of list.

Yes you can always filter by tag, if (x.tag == "spaceship") or whatever, but then you have to remember to always have the tag there, plus safety line of code in case tag missing to stop it crashing, so if you can do what you need with just the array or list index that'd be even better.

I'd google "Unity array list use cases" to see any pointers, but the idea is to use profiler when game gets to certain size (near the end stage of development I think is the idea!) so you actually see which parts slow you down, and focus on them instead of wasting time splitting hairs over a size 16 array or list. also Unity docs has a tutorial on object pooling but I couldn't see where the code is on the page, so I chose this one as all the code is neatly displayed on page. I think it's quicker with article than video in the long run to really digest the meaning of it all.

Comment
Add comment · Show 1 · Share
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 captPivoine · Oct 26, 2018 at 07:12 PM 0
Share

Right.. Thanks a lot!.. Plenty of new terms and concepts in your answer for me to look up.. The final answer may very well be in there, so thanks!.. Couple of things that maybe will clarify the actual situation: - there s only a fixed number of spawnSpots by map/level, according to difficulty level. Eg: 4 at low level, up to 16 at higher/ more difficult levels, but the amount of spots within each map do not change. They are not created or destroyed on the fly, they exist in each map and are dropped in as the elements of the spawnSpots array, in the map s script component, in the editor. - the game spawns at interval according to difficulty (like, every 5 sec at low level, every sec or 0.5 sec at higher level) by randomly picking one of the possible SpawnSpots of the map. - At the moment, the SpawnSpots objects can detect if they are occupied (with collider), which change their tag from "free" to "occupied" (and back if collision is null) -At the start of the game, all the SpawnSpots of that map are active.. And they should become inactive while occupied, and then become active again.. Just so that nothing spawns on an already occupied spot

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

95 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

Related Questions

C#, code stops loop prematurely 1 Answer

Get all GameObjects by variable value 2 Answers

Play Animation Elements List 0 Answers

Using arrays to play music with level loading doesn't seem to work. 1 Answer

Crate an array associative from a php return array 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