DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Architecting Unity3D – The dreaded “.”

Architecting Unity3D – The dreaded “.”

Simon Jackson user avatar by
Simon Jackson
·
May. 17, 14 · Java Zone · Interview
Like (0)
Save
Tweet
4.06K Views

Join the DZone community and get the full member experience.

Join For Free

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

https://www.youtube.com/watch?v=tsfakmew0lw

later’’s

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Deploy Apache Kafka With Kubernetes
  • Building a Kotlin Mobile App with the Salesforce SDK, Part 3: Synchronizing Data
  • The Most Popular Technologies for Java Microservices Right Now
  • The 5 Healthcare AI Trends Technologists Need to Know

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo