Hello. I really need a good way to get only the objects of a particular layer in a particular scene. Basically I need a way to tell if an object is in that scene or not. I woud rather not have to Find all objects in the game when I do this or use tags. Rather, I would like to be able to just have a scene manager that does it.
Well, depending on how often you will do that, you can actually use findObjectsOfType, or it may be faster to get specific scene, get root gameObjects and iterate over transform hierarchies collecting everything with specific layer. If your scene don’t contain a lot of unrelated objects, like UI, it can be relatively cheap.
If you need to do it frequently and fast then you should actually think about how you store your data. One of solutions I could think of is simple component which you add to every gameObject and which will add itself to some list.
If you don’t want to do that manually and assuming you instantiate rarely, you can combine first and second approach, and add components on start and after object instantiation which will move all expensive stuff to initialization phase and the rest of the frequent queries will be relativly cheap.
Thanks a lot, The scene thing is actually exactly what I need. It would actually just be when the scene first loads, too. If it wasn’t for modders, I would probably try to add them pre-compile. I may even still do that. I’m guessing that’s not worth a whole script on every object?
If it works and performs well (scene isn’t loading too long for example) then I would go with dynamic component attachment. It’s the quickest option so I would start from that, you can change it later quite easily if you want.
Dynamic component attachment? As in, I drop a component onto specific objects I want to mark, then find the component specifically? Hmm. I’m sure that would work with something small, but unfortunately these are tiles in a 3D game - so there will be literally thousands of them by the time I’m done. Then again, they’re all colliders, so that might narrow it down slightly at least.