More efficient to have a simple script on many game objects or a list of game objects and cycle through it?
Hello,
Let's imagine I have 500 wall sections and I want to track the health of each wall section.
Do you know if it is more efficient to:
1/ add a script to each wall section game object containing a health variable (and no update() part)
2/ have a list of wall class with GameObject and Health data and then use a find function to find the wall in the list when interacted with.
Would the answer remain the same if I had for example 100,000 tiles... when is a list too long. If 100,000 and option 1 - it would be possible to deactivate large counts of the 100,000 tiles (e.g. not on screen).
I did some brief research but didn't find anything related to this - apologies if I miss something.
Any thoughts or suggestions?
Thanks,
Answer by Hellium · Mar 06, 2019 at 05:49 PM
Unity has published a blog post about your concerns.
If you know you will have a large amount of entities, then, use a manager handling them (in a list, or in a dictionary with instanceID of the gameObjects as key and a struct containing the gameObject and the health as value for instance).
I am not familiar enough with Entity-Component-System, but it may fit your needs here.
Would it be possible to deactivate large counts of the 100,000 tiles (e.g. not on screen).
Even if a tile is off-screen, I believe enemies can still attack those tiles, can't they?
This is not exactly what I am looking for as it related to update() - I'm looking more for information storage. A script without Update() on each object - versus the list/dictionary. But what you posted is an interesting read - thanks - and I do believe that lists will be more efficient.
Your answer
Follow this Question
Related Questions
How to optimize this extremely repetitive list script? 0 Answers
Regex Efficiency Question 0 Answers
Is it really inefficient to move objects that aren't rigidbodies? 1 Answer
More efficient way to compare objects in this manner? 0 Answers
Space shooter tutorial scrolling background: which is the better way of implementing it? 2 Answers