Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Architecting Unity3D – The Dreaded “.”

, 12 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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   There are however a few behind the scenes changes …

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.

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 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.

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.

Later’’s.

License

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

Share

About the Author

Simon Jackson
Architect ZenithMoon Studios
United Kingdom United Kingdom
Long time game developer / IT maniac.
By day working as a lowly Technical Architect for a healthcare software firm, by night Masquerading as the Master Chief of ZenithMoon Studios.
 
At heart I'm 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 AdRotator advertising rotator project for Windows and Windows Phone.
 
After a hefty break with Windows Phone and Windows 8 I'm heading back to my XNA roots to show how beginners and current XNA devs can take their skills forward with XNA's bleak future.
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionIts really new for me. :) PinmemberNabeel Saleem4-Sep-14 5:43 
QuestionPubcenter or Adrotator in Windows Store Unity3D game. PinmemberTomDev828-May-14 9:29 
AnswerRe: Pubcenter or Adrotator in Windows Store Unity3D game. PinmemberSimon Jackson29-May-14 1:27 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. PinmemberTomDev829-May-14 4:16 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. PinmemberSimon Jackson29-May-14 4:36 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. PinmemberTomDev829-May-14 4:53 
AnswerRe: Pubcenter or Adrotator in Windows Store Unity3D game. PinmemberSimon Jackson29-May-14 5:45 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. PinmemberTomDev829-May-14 6:56 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. PinmemberTomDev829-May-14 8:15 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. PinmemberSimon Jackson29-May-14 9:34 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. PinmemberTomDev830-May-14 2:30 
GeneralRe: Pubcenter or Adrotator in Windows Store Unity3D game. PinmemberTomDev83-Jun-14 6:16 
AnswerRe: Pubcenter or Adrotator in Windows Store Unity3D game. PinmemberSimon Jackson18-Jun-14 9:40 
GeneralGreat Advice. PinmemberFlickayy17-May-14 15:28 
GeneralRe: Great Advice. PinmemberSimon Jackson19-May-14 2:30 
QuestionNice Simon! PinprofessionalVolynsky Alex12-May-14 9:04 
AnswerRe: Nice Simon! PinmemberSimon Jackson12-May-14 9:33 
GeneralRe: Nice Simon! PinprofessionalVolynsky Alex12-May-14 10:26 
GeneralVideo Embedding is not Working! PinmemberRavimal Bandara11-May-14 7:12 
GeneralRe: Video Embedding is not Working! PinmemberSimon Jackson11-May-14 9:59 
Questionwhich scripting language ? PinmemberGiacomo Pozzoni10-May-14 23:59 
AnswerRe: which scripting language ? PinmemberSimon Jackson11-May-14 1:34 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 12 May 2014
Article Copyright 2014 by Simon Jackson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid