Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Berenger · Mar 13, 2012 at 07:18 PM · gameobjectperformancewebplayerchildrencomponents

Does a large number of components will affect performance ?

Hi !

When you have a important gameobject, such as the player, does a large number of components affect the performances ? I'm talking about around 10 - 15 here.

I try to have very specific scripts for clarity and reusability, so right now I have MeshFilter - Collider - Renderer - Char Contr. - Char Motor - A generic class for interactions - Inputs - Seeker (path finding) - Inventory - All sorts of GUI - etc.

All that on a single GameObject. Is it a bad idea ? Would it be better to dispatch them on children ? What if there is even more ?


PS : This is for webplayer / standalone.

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
4
Best Answer

Answer by JDPennock · Mar 14, 2012 at 06:24 AM

It really all depends. The things that are going to affect performance the most in this type of situation are the monobehavior functions that get called every frame (Update) or even multiple times a frame (OnGUI). So if every single one of your components inherit from monobehavior and each have their own Update, OnGUI, ect, functions yes this is going to affect performance some. And even then breaking them out to child objects is not really going to help because you will still have the same number of calls to update ect every frame.

So how do you combat this? Generally with a mixture of, creating a careful inheritance structure to your classes and if needed a little bit of state based programming.

For instance for something like a player character object, You may want to create a component called something like Player or PlayerController or similar and use it as the main place where update and OnGUI are called for your player. It could perhaps inherit from your Seeker component, which could in turn have it's own instances of Char Motor and a class for interactions that are just generic classes that are not derived from monobehavior. What used to be in the update functions for these components could be renamed to more descriptive things and controlled more directly from within the update of your Seeker or PlayerController classes. This also can help cut down on message passing between components. Designing this way you could also make an NPC controller that also inherits from Seeker but perhaps does other things and doesn't have an instance of a generic input class. I hope this is making some sense, in general you are trying to compound a bunch of your components into one through careful inheritance and instances of generic classes that don't inherit from monobehavior. But even though you end up with one or two or so main components your code is separated into many files and reusable classes. Creating these long inheritance chains for complex objects is pretty common in many areas of game development. For example if you are ever working with the Unreal engine half the battle is studying the chain of inheritance that is already there and deciding where to break it and start writing your own subclasses. Designing these inheritance chains is the real meat of object orientated programming. A lot of folks around these forums talk a lot about object orientated design and think they know a lot about it because they have a bunch of gameobjects each with their own set of unique JavaScript components but thats really more of a behavioral programing approach then an object oriented one.

For your all sorts of components for GUI stuff you could use a more state based approach. Say each component implements a different menu screen or something. Instead of letting each one have their own OnGUI function and constantly checking whether or not they should be drawing somewhere inside of that OnGUI function you could instead take all that code you would put in OnGUI and rename it to a function like Draw. Then you could call a particular menu components draw function from inside of the OnGUI function of your PlayerController based on what menu state the player was in.

So this is a pretty long answer, but like I said in the beginning it really all depends on what you are doing. If you have have an object with 15 or so components but really only one 2 or 3 are controllers with updates being constantly called and the rest are just some sort of gui stuff that is just siting there waiting to be run in an OnGUI call, you are probably fine. If you have 15 components all with their own updates doing their own things you may want to rethink you design a bit. But then again if there are not very many complex things going on in your scene it could be just fine too. It really all depends on the project and situation.

Also don't take my comments above about behavioral programing too seriously. No one programming approach is always better then the other. Sometimes a quick JavaScript behavior is a better idea, sometimes a complex inheritance chain of C# classes is better. The best way to learn what works were is to keep designing things and trying out your ideas to see if they work.

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 Berenger · Mar 14, 2012 at 08:49 PM 0
Share

Ok I think I get the point. I need to separate the "What it does" and concentrate the "When it's doing it" (Updates and other OnGUIs). Thank you for your time !

avatar image Berenger · Mar 17, 2012 at 11:56 PM 0
Share

I am now using an interface IGUI with a Draw function (among other things) implemented by every monobehaviour using GUI. At start, a singleton gather all those objects then draw all of them in it's OnGUI function.

Does that sound loke the right path to take ?

avatar image
1

Answer by Hamesh81 · Mar 13, 2012 at 07:38 PM

I think this is the answer you are looking for buddy:

http://answers.unity3d.com/questions/52234/one-large-script-or-break-it-up-into-many.html

Comment
Add comment · Show 1 · 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 Berenger · Mar 13, 2012 at 08:16 PM 1
Share

Thanks for the answer.

In that post though, they are speaking of splitting the code for clarity which I totally agree with. I am more worried by the accumulation of several components on the same object.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Whats the best workaround for finding Gameobjects at Start? 1 Answer

Detect 2D GameObjects when "Colliding" with each other without RB and collider. 1 Answer

Disable/Enable Script or Add/Remove? 1 Answer

Adding ChildObject 2 Answers

Conversion of GameObject with Component to Specific Type 0 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