x


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é

more ▼

asked Sep 07 '11 at 12:06 PM

andresp gravatar image

andresp
183 18 21 25

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Sep 07 '11 at 12:15 PM

Waz gravatar image

Waz
6.4k 22 33 71

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

Sep 07 '11 at 06:34 PM andresp

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. My 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.

Sep 08 '11 at 10:52 AM Waz
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Sep 07 '11 at 06:51 PM

StephanK gravatar image

StephanK
6k 39 53 93

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5081
x4161
x2088
x302
x124

asked: Sep 07 '11 at 12:06 PM

Seen: 914 times

Last Updated: Sep 08 '11 at 10:53 AM