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 your code work better.
The first part is to understand your code and to make it better, so that you think about:
- Do I really need to keep referencing a component in 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
in 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.
These are just simple changes to how you use and access components within the Unity3D system.
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 Future is Bright, the Future is Unity5
Unity 5 is fast approaching bringing with it a whole heap of new features, some obvious, some not so obvious.
If you haven’t seen what’s coming publicly in Unity 5, check out the feature preview shown at Unite 2014.