I have a quite large set of building models which I import to the scene, creating the respective GameObjects.
Although every model is unique, conceptually these GameObjects are quite similar (they are all houses), in a way that they share the most part of their interaction (picking handling, data...). Typically, in an object-oriented language, this kind of similarities would imply the creation of a "House" class where all the common features would be stored.
However, Unity based its structure in the attachment of specific scripts to the GameObject. In this case, where I would have to attach the script to some hundreds of entities, this approach seems very strange and unnatural, so I just wanted to make sure that this is the only way to do it.
Is there any other Unity's concept that can be used to achieve this functionality?
Thanks in advance, André
asked Sep 07 '11 at 12:06 PM
It depends on the rest of your code, but nothing is stopping you creating the GameObjects at runtime, from your models, with AddComponent attaching the House script.
It's really no difference to any other OOP, since you would still have to derive subclasses or assign the model data to a House instance in any case.
Once you've used it for a while, you'll see how the OO component composition model in Unity is actually quite awesome, and perfectly natural. My advice would be to go with the flow and don't try to square the hole. Once you're an expert with Unity, then you can better apply your expertise from other areas.
answered Sep 07 '11 at 12:15 PM
You can write an Editor script that will automatically assign your house script to all selected houses. There's even an example of how to do that in the Unity script reference. I think it was on the MonoScript.GetClass page. Depending on how different your houses are it may also make sense to create a prefab of a house and change only the parameters of your instances that should differ from the original.
answered Sep 07 '11 at 06:51 PM