Hi, I need to check state of few objects. What is better, attached script with check option to all objects or check it in one script in foreach loop?
Generally I try to put code controlling an object on the object. That makes it much easier to understand why an object is behaving as it does. Especially when you have to look at it a month or two later. One large script is generally harder to decipher than several small and targeted ones. That said, I could envision situations where you would want to test the state in one separate script. It's really up to you.
Depends on how many objects there is. If there are just a few objects, (like 5-10) just put Update scripts on them. If there are hundreds of objects, don't be putting Update scripts on all of them, I'm pretty sure that'll kill performance. It'd be better to make a List of the objects and check them in a for loop. If there are thousands of objects, you may want to have the for loop run over several frames. Here's an example of how I did this in one of my projects, where I have thousands of pre-determined chunks that need to be checked. Code (CSharp): public int chunkIndex = 0; public int checksPerFrame = 200; void Update() { for (int i = 0; i < checksPerFrame; i++) { if (chunkIndex + i < chunkMarkers.Count) { //do something with the current object e.g. chunkMarkers [chunkIndex + i].DoSomething(); } } chunkIndex += checksPerFrame; if (chunkIndex >= chunkMarkers.Count) { chunkIndex = 0; } }