- Home /
Is FindGameObjectWithTag() too heavy on resources for 50 objects?
Hi,
I have about 50 objects to which I appended an empty child-object. I store the coordinates of the object as a tag in that child-object. The tags will be something like "1,4", "2,3", "6,1" etc...
My game will search one tag through these 50 objects every 1 second. Is this too heavy for a mobile game?
Is there an alternative to tags for my purpose? Is there a way of using integers instead of strings to make it faster?
EDIT: my 50 game objects are prefabs and appear progressively during the game. So they are not there from the start. I want to track each of these prefabs with an integer and destroy some of them at will during the game
I predict that it is efficient enough for what you want or even if you wanted to do it every frame. But why take my educated guess? For situations like this, I just test it. Usually I keep pushing until I get a slow down. Create a sample app with with your data. Execute the code every frame. If that doesn't slow it down, start executing the code in a loop many times each frame.
While I agree with robertbu, I also think your design use of "tags" to store stuff like coordinates sounds funky. Why not use a List which can be iterated over?
Yes, you should re-think your design. You don't really need to use any of the Find commands more than once (in Awake/Start)...after the list is populated initially, have a system where objects register/unregister with the list as needed, so you don't need to do Find again.
id say, cos each time its doing all that string data parsing and I think each call is a long trudge through every object in game`s tag identifier until you find you objects in question. I wonder if it would be more productive to at first find them and populate a list with them and then you already have them set up to play with.. this would both reduce your overheads and rework probably even help to shorten your code. Ooops, sorry Eric5h5, i have just noticed you have practically said the same thing but more succinctly. doh . Gruffy :) (actually, i think there might be common trait in all the comments above here)
Hi there, thank you for your responses. I have read some threads about lists and they seems to be a C# feature, and I am using Javascript. Also, I already have arrays for my 50 objects and they are fixed-sized. Do you think I can efficiently do the same with JS arrays or is it worth making lists in a separate C# script?