Over a million developers have joined DZone.

Architecting Unity3D – The dreaded “.”

DZone 's Guide to

Architecting Unity3D – The dreaded “.”

· Java Zone ·
Free Resource

So what is in a “.”

When you use components and scripts in your Unity3D project and then want to access them in code, it is very easy to just start writing:

this.renderer.collider.attachedRigidbody.angularDrag = 0.2f;

(Yes, that is a bit of an extreme case but it’s there to prove a point)

In that simple one line of code you have actually invoked or accessed 3 separate components attached to the GameObject you are running the script from.

In the background, Unity3D converts this to:

var renderer = this.GetComponent<Renderer>();
var collider = renderer.GetComponent<Collider>();
var ridgedBody = collider.GetComponent<Rigidbody>();
ridgedBody.angularDrag = 0.2f;

Not so simple now is it.  This process is inherently slow and can sometimes involve a fair bit of reflection in the code (process of dissembling code in memory, like any of the Function(string) methods do).

Multiply that by every frame and you can start to begin to see why this is such a problem, it was just a way to write simpler code in the beginning but as it is becoming more prevalent these days unity has said “enough is enough” and is making breaking changes.

Note, AFAIK, the behaviour also affects both JavaScript and Boo as well.

Best practice with Unity3D

This behaviour with changing components together in code using DOT notation does work but like so many things, “just because you can do a thing doesn't mean you should”, so what can you do to make you’re code work better.

Part of this is to understand your code and to make it better, so that you think about:

  • Do I really need to keep referencing a component each frame
  • If you do, then keep that component as a reference in your script to access it rather than calling GetComponent every time
  • You can still do GetComponent each frame if you wish (not advised), you are just in control of it now.

So I would convert the above script to:

Rigidbody myScriptBody;
void Awake()
    var renderer = this.GetComponent<Renderer>();  
    var collider = renderer.GetComponent<Collider>();  
    myScriptBody = collider.GetComponent<Rigidbody>();
void Update()
    myScriptBody.angularDrag = 0.2f * Time.deltaTime;

Thus only using GetComponent the first time the script is run and storing the reference (or references) I need at run time, then using the in memory reference instead of trying to discover it every frame.

The best advice

You are now forewarned and forearmed with new knowledge, use this knowledge to change your current practices if you have been using the “.” method in your code.

Keep it in mind with every video tutorial you watch or script you import from a lib or wiki and adapt it in advance.

If you currently are doing this a lot in your code and feel it’s too much of a change for what is arguably just a modularisation of your code, then keep your ears out for Unity5, there’s still quite a few surprises yet to come, including some help with this behaviour.

The best advice

Unity 5 is fast approaching it being with it a whole heap of new features, some obvious, some not so obvious.

If you haven’t seen what’s coming publically in Unity 5, check out the feature preview shown at Unite 2014




Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}