Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by andresp · Sep 07, 2011 at 12:06 PM · c#gameobjectcomponentclasses

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é

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andresp · Sep 07, 2011 at 06:34 PM 0
Share

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

avatar image Waz · Sep 08, 2011 at 10:52 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 Answers

C# Adding Components From Other Gameobjects 3 Answers

C# Check If Gameobject is within Collider 1 Answer

How to Check if a component Exists on a Gameobject. 3 Answers

press e to speek -1 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges