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

It’ll have a direct impact on ram, but that’s it. If it’s only a thousand objects or so I hardly think it will matter.

Performance won’t change unless of course you instantiate, then things might get downright dirty.

Ok Thanks for the answer that’s really helpful!

So if for example I instantiate objects on Start, this could have an impact ?

Yeah but that’s in start. It’s going to take a huge amount to to even slow down start. It’s bad during gameplay but not in start.

Perfect thanks a lot!

And what is your opinion? do you think it’s a good practice? Should I take all the components to override them ? Just somes ? Or do it specifically in all my scripts ?

I’m not sure why you would want to do this in unity 4. It makes no difference in Unity 4. In Unity 5, you’ll need to use GetComponent.

No I mean on awake I do something like _transform = base.transform.