Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / Javascript

Architecting Unity3D – The Dreaded “.”

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
12 May 2014CPOL3 min read 23.5K   4   21
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 publicly in Unity 5, check out the feature preview shown at Unite 2014. There are however a few behind the scenes changes …

So What Is In A “.”

Image 1

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:

C#
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:

C#
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:

C#
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

Image 2

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

Image 3

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.

Image 4

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect ZenithMoon Studios
United Kingdom United Kingdom
Long-time game developer / IT maniac.
By day I architect, design, build and deliver enriching Mixed Reality solutions to clients, bringing the work of AR/VR to light in new and interesting ways, by night I Masquerade as the Master Chief of ZenithMoon Studios, my own game development studio.

At heart, I am a community developer breaking down lots of fun and curious technologies and bringing them to the masses.

I'm also a contributor to several open-source projects, most notably, the Reality Toolkit and all the services provided by the Reality Collective, The Unity-UI-Extensions project, as well as in the past the AdRotator advertising rotator project for Windows and Windows Phone.

Currently, I spend my time fulfilling contracts in the Mixed Reality space (primarily for an XR experience firm called Ethar), writing books, technically reviewing tons of material and continuing my long tradition of contributing to open-source development, as well as delivering talks, but that goes without saying Big Grin | :-D

Mixed Reality MVP, Xbox Ambassador, MS GameDevelopment Ambassador & Best selling author:

[Accelerating Unity Through Automation](https://www.amazon.co.uk/Accelerating-Unity-Through-Automation-Offloading/dp/1484295072/ref=rvi_sccl_3/262-0817396-1418043)
[Mastering Unity 2D Game Development] (https://www.packtpub.com/game-development/mastering-unity-2d-game-development)
[Unity 3D UI Essentials] (https://www.packtpub.com/game-development/unity-3d-gui-essentials)

Comments and Discussions

 
QuestionPubcenter or Adrotator in Windows Store Unity3D game. Pin
TomDev828-May-14 8:29
TomDev828-May-14 8:29 
AnswerRe: Pubcenter or Adrotator in Windows Store Unity3D game. Pin
Simon Jackson29-May-14 0:27
Simon Jackson29-May-14 0:27 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. Pin
TomDev829-May-14 3:16
TomDev829-May-14 3:16 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. Pin
Simon Jackson29-May-14 3:36
Simon Jackson29-May-14 3:36 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. Pin
TomDev829-May-14 3:53
TomDev829-May-14 3:53 
AnswerRe: Pubcenter or Adrotator in Windows Store Unity3D game. Pin
Simon Jackson29-May-14 4:45
Simon Jackson29-May-14 4:45 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. Pin
TomDev829-May-14 5:56
TomDev829-May-14 5:56 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. Pin
TomDev829-May-14 7:15
TomDev829-May-14 7:15 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. Pin
Simon Jackson29-May-14 8:34
Simon Jackson29-May-14 8:34 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. Pin
TomDev830-May-14 1:30
TomDev830-May-14 1:30 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. Pin
TomDev83-Jun-14 5:16
TomDev83-Jun-14 5:16 
AnswerRe: Pubcenter or Adrotator in Windows Store Unity3D game. Pin
Simon Jackson18-Jun-14 8:40
Simon Jackson18-Jun-14 8:40 
GeneralGreat Advice. Pin
Flickayy17-May-14 14:28
professionalFlickayy17-May-14 14:28 
GeneralRe: Great Advice. Pin
Simon Jackson19-May-14 1:30
Simon Jackson19-May-14 1:30 
QuestionNice Simon! Pin
Volynsky Alex12-May-14 8:04
professionalVolynsky Alex12-May-14 8:04 
AnswerRe: Nice Simon! Pin
Simon Jackson12-May-14 8:33
Simon Jackson12-May-14 8:33 
GeneralRe: Nice Simon! Pin
Volynsky Alex12-May-14 9:26
professionalVolynsky Alex12-May-14 9:26 
GeneralVideo Embedding is not Working! Pin
Ravimal Bandara11-May-14 6:12
Ravimal Bandara11-May-14 6:12 
GeneralRe: Video Embedding is not Working! Pin
Simon Jackson11-May-14 8:59
Simon Jackson11-May-14 8:59 
CodeProject doesn't allow video embedding unfortunately (or at least I've not found a way to)
I have updated the article to make it look better, it's just waiting approval.

you can see the video here for now - https://www.youtube.com/watch?v=tSfakMeW0lw
Questionwhich scripting language ? Pin
Giacomo Pozzoni10-May-14 22:59
Giacomo Pozzoni10-May-14 22:59 
AnswerRe: which scripting language ? Pin
Simon Jackson11-May-14 0:34
Simon Jackson11-May-14 0:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.