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

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.