Unity3D: Public Variables

One of the reasons I switched to Unity3D was because of this cool feature within the editor:

Camera Script based on 3DBuzz's Third Person tutorial

ANY Public variables you make can be changed from inside of the editor and all scripts can be applied to an object. I can manipulate my Third Person Camera settings from within the editor without having to change any pieces of script. So one the script is done, I don’t have to touch it again. And because these variables are public, I can have another completely different script change these variables, so in the above example, if I had an options menu I could adjust any sensitivity settings that may improve the player’s experience with the mouse. It’s also partially why I’m loving this object-based programming, whilst in Dark Basic Professional I can set ‘global’ and ‘local’ variables, it seems types within an OOP language like C#, you’ve got a lot more control. A private variable only needs to be used in a single class, but DBP doesn’t have classes and it’s closest thing is its local variables, but you can’t even use those local variables in multiple methods without making it global.

This kind of thing is useful here:

Now I’ve got some player stats down, perhaps the labels should be a little descriptive but these (beside Level, HP, MP, ATB/MaxATB, XP and Class) are only meant as the default ‘level 1’ stats when changed in editor, which can be calculated when the level increases. For example, at level 5 you’re looking at 100hp and 50mp. But these are stored in the private variables and these private variables update internally and use the private variables for the maths and updates the public variables with the new information (public variables which might be accessed by the GUI).

So when running the game, the stats update, but also, because they’re updated from within the Update() method, I can just increase the level in editor whilst the game is still running and the stats will automatically update. This highlights another bonus within Unity3D, you can run the game an adjust variables on the fly to see what adjustments work best.

These variables are defined in a fairly straight forward manner, because C# is case sensitive I can actually get away with repeating variable names, which is something else you don’t get in DBP.

The variables are just all set for editor defaults. Casing just allows to define the difference between the ‘public’ and ‘private’ versions. The reason I’m using a private variable is to set a default variable that’ll be stored on ‘Awake()’ (when it’s loaded), hence you’ll see the method ‘StoreStats()’, it’s only called there because it stores those values defined in the editor and then ‘UpdateStats()’ will apply the mathematics, which is called in ‘Awake()’ so HP/MP can be restored to full (the reason this won’t be done in an update() is because you could never take HP or MP away, it’s just reset to max every frame). As it’s possible to see, private variables have their uses.


So already there’s some Unity3D features that make life much easier and allow for adjustments to be a lot quicker.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s