Naming your variables properly

Always use meaningful names for storing your variables. If you don't do that, six months down the line, you will be sad. I'm going to exaggerate here a little bit to make a point. I will name a variable as shown in the following code:

public bool theBearMakesBigPottyInTheWoods = true;

That's a descriptive name. In other words, you know what it means by just reading the variable, and so ten years from now when you look at that name, you'll know exactly what it means. Now suppose instead of theBearMakesBigPottyInTheWoods, I had named this variable as shown in the following code:

public bool potty = true;

Sure, you know what potty is, but would you know that it referred to a bear making a big potty in the woods? I know right now you'll understand it because you just wrote it, but six months down the line, after writing hundreds of other scripts for all sorts of different projects, you'll look at that and wonder what potty meant. You'll have to read several lines of code you wrote to try to figure it out.

You may look at the code and wonder who in their right mind would write such a terrible code. So take the time to write a descriptive code that even a stranger can look at and know what you mean. Believe me, in six months or probably less, you will be that stranger.

Begin variable names with lowercase

You should begin a variable name with lowercase because it helps to distinguish between a class name and a variable name in your code. The Component names (class names) begin with a capital letter. For example, it's easy to know that Transform is a class, and transform is a variable.

There are of course exceptions to this general rule, and every programmer has a preferred way to use lowercase, uppercase, and perhaps an underscore to begin a variable name. At the end, you will have to decide upon a naming convention you like. If you read the Unity forums, there are some heated debates on naming variables. In this book, I will show you my preferred way, but you can use whatever is more comfortable for you.

Using multi-word variable names

Let's use the same example again as follows:

public bool theBearMakesBigPottyInTheWoods = true;

You can see that the variable name is actually eight words squished together. Since variable names can be only one word, begin the first word with a lowercase, and then just capitalize the first letter of each additional word. It greatly helps to create descriptive names and still being able to read it. There's a word for this called camelCasing.

Have a go hero – viewing multi-word variables in the Inspector panel

I already mentioned that for public variables, Unity's Inspector will separate each word and capitalize the first word. Go ahead, add the previous statement to LearningScript and see what Unity does with it in the Inspector panel.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.78.83