- Home /
Classes and Scripting
Hi
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?
P.S. I'm using C# but I can use Javascript too
Thanks in advance, André
Answer by Waz · Sep 07, 2011 at 12:15 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.
thanks for your answer, but creating the GameObjects at runtime would imply that the editor view would be empty and a little bit useless while not running the game, right? that would remove many advantages of Unity's visual experience while developing the game like positioning, baking lightmaps, quick access to the inspector properties... Automatically attaching the needed scripts to the set of gameObjects could be done by using some custom Editor scripts, but that seems unnatural too (e.g. if we happen to do a mistake then we would have to create a new script to remove the specific script component...). Also, if we want to have some kind of metadata about the model to be used by several gameobject components, we would have to create a "Data Component" script whose fields would have to be accessed by using the GetComponent function. The fields or the get/set methods would have to be public too. I think I'm probably not understanding the entire structure of Unity as this seems really odd to me. I guess I should read more about the typical code structure of an unity's game. Do you happen to know where I can find some quality content within this field? thanks again
Sounds to me like you're already seeing the point of Unity's component model!
The "empty scene" thing will happen for any game with randomly generated levels/maps. $$anonymous$$y current project is like that - but the prefabs are still all built visually, even if once completed they are delete from the scene.
As for examples, the Tutorials from Unity are usually quite good like this, sometimes even deliberately over-doing the component breakdown in order to illustrate its value.
Answer by StephanK · Sep 07, 2011 at 06:51 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.