I have like 100 doors in one scene and I need to write a script to open and close them. Should I attach 100 scripts to the objects or is it better to attach 1 script to the player and ask in the collision event if it is a door and do the logic there?
I want to automatically close the doors after 5 seconds, so I have to have some list of door objects in the player script and check every door in this list to close it after 5 secs.
But this extra door handling is still better than attaching simple door-scripts to each door I guess?
You have to keep a list of objects somewhere right? Why not just use Unity’s built-in list of objects and attach the script to each of them? Presumably that would be part of a prefab.
The code itself isn’t duplicated 100 times - just any data that’s part of your script class. This data is state you’d have to maintain yourself (your list of objects, the timers, etc.) so it makes sense to keep that data in the door script and attach it to the doors.
This is exactly what the Prefab feature is for. It allows you to create a single master object and clone it around your scene. Whenever you edit the prefab (master object), the clones are automatically updated. The clones also have a button in their Inspector tab that allows you to send changes to a clone back to the master–and out to all other clones.
Prefabs are explained starting at 4:16 in this video: