Fun with Exposed Variables in Unity

At first I wasn’t thrilled about public variables being exposed in Unity’s editor – it seemed to be a bit dangerous, burying variable within GUI elements. Reminded me too much of hunting through Flash MovieClips for code snippets. And if you’re not careful, you will get burned. For example, public int exposedInt = 100; at the top of your class rather than assigning the variable within the constructor can cause considerable frustration if the variable is re-assigned via the exposed interface.

That said, once I got used to exposed variables, the ease and speed of development using them had me convinced of at least some merit. No more GameObject.Find() not finding.

It wasn’t until a while later during the development of AI Apocalypse that we came across some very handy ways in which the Unity GUI handles other exposed data types. For example, and exposed Array of GameObjects:

public GameObject[] Parts;

Will give you an empty array for which you can set the size within the editor and then just drag GameObjects into each element.

Unity GameObject Array

Unity GameObject Array

And an exposed enumeration is represented in the editor as an intuitive combo box:

public enum RobotType
{

    Other = 0,
    Tank = 1,
    RoundTank = 2,
    TrackTank = 3

}

unity-editor-enumeration

unity-editor-enumeration

Ok, maybe my definition of fun is a little lame, but these tricks have definitely saved a whack of time.

If you liked it, share it:

  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • blogmarks
  • DZone
  • Identi.ca
  • LinkedIn
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter

There are no comments, yet.

Why don’t you be the first? Come on, you know you want to!

Leave a Comment