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
0
Question by InvincibleCat · Jan 20, 2015 at 12:00 AM · performanceoptimizationgetcomponentperformance optimization

Unity Best Practices - Cached Components

Hi everyone,

As I'm working on mobile, optimization is an important part. I'm trying to find the best way to automatically assigned most of the components properties as followed:

 public abtract class MonoBehaviourBase : MonoBehaviour
 {
     private Animation _animation = null;
     public new Animation animation
     {
         get { return _animation; }
     }
 
     private AudioSource _audio = null;
     public new AudioSource audio
     {
         get { return _audio; }
     }
 
     private Camera _camera = null;
     public new Camera camera
     {
         get
         { return _camera; }
     }
 
     private Collider _collider = null;
     public new Collider collider
     {
         get
         { return _collider; }
     }
 
     private Collider2D _collider2D = null;
     public new Collider2D collider2D
     {
         get { return _collider2D; }
     }
 
     private Transform _transform = null;
     public new Transform transform
     {
         get { return _transform; }
     }
 
     private ConstantForce _constantForce = null;
     public new ConstantForce constantForce
     {
         get { return _constantForce; }
     }
 
     private HingeJoint _hingeJoint = null;
     public new HingeJoint hingeJoint
     {
         get { return _hingeJoint; }
     }
 
     private Light _light = null;
     public new Light light
     {
         get { return _light; }
     }
 
     private ParticleEmitter _particleEmitter = null;
     public new ParticleEmitter particleEmitter
     {
         get { return _particleEmitter; }
     }
 
     private ParticleSystem _particleSystem = null;
     public new ParticleSystem particleSystem
     {
         get { return _particleSystem; }
     }
 
     private Renderer _renderer = null;
     public new Renderer renderer
     {
         get { return _renderer; }
     }
 
     private Rigidbody _rigidbody = null;
     public new Rigidbody rigidbody
     {
         get { return _rigidbody; }
     }
 
     private Rigidbody2D _rigidbody2D = null;
     public new Rigidbody2D rigidbody2D
     {
         get { return rigidbody2D; }
     }
 }

But I was wondering if declaring all this Components would affect the size of the objects and so have a direct impacts on the performance?

Thanks

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by SilentSin · Jan 20, 2015 at 12:52 AM

Yes, that would affect the size of every script that inherits from it. Everything now has to have 14 extra references as well as an extra layer of inheritance.

If a meaningful optimization could be achieved by structuring a class like that, they would just have structured MonoBehaviour like that. And I'm pretty sure they're actually doing something like that anyway. I haven't tested it in a few years, but using .transform in your script is actually quite a bit more efficient than calling GetComponent(), and only a tiny bit less efficient than having your own proper reference to the transform.

Just because you're on mobile, doesn't mean you need to optimize everything. You should only optimize things like this when you actually start running into performance problems, and this particular solution is probably not going to solve any such problem.

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 InvincibleCat · Jan 20, 2015 at 12:57 AM 0
Share

Ok thanks, that's clear!

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

26 People are following this question.

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

Related Questions

Alternative to getComponent? 3 Answers

Bad performance on Android 0 Answers

gfx.waitforpresent took up to 40ms even without any gameobject running 0 Answers

Great FPS Dilemma Even With Low Tris & Low Batches 0 Answers

Optimize mesh for runtime performance 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