String “auto-completion”, Tags and Hash IDs

Unity Tags, Layers and Animator components are great tools to use, but they have one drawback: It is necessary to pass strings into certain MonoBehaviour methods to make them work.

Consider this short example:

There are several situations in which you can’t get around using strings as parameters for certain Unity methods. Other times it might be the most convenient and practical solution.

At this point it would be too much to talk about all the implications of using strings and how or why to avoid them. Instead, I’d like to show you a remedy for one of the many downsides of using strings in functions.

Strings as method parameters are difficult to deal with because of possible spelling-errors or tedious renaming of multiple string-instances.

The most common examples of string usage in methods are:

  • Output messages for console logging, warning messages and exceptions.
  • Accessing Animator layers and parameters
  • Identifying objects by Tag
  • Calling a method by name when using Invoke() or a Coroutine

A good solution for working with anything Animator related is to hash strings to integers and feed the Animator with ID’s instead of layer and parameter names. Int variables are much better for performance than strings. They also have the benefit of being visible in IntelliSense.

Create this script and attach it to a GameObject in the scene (Awake must be called for the StringToHash() method to create our ID’s):

I prefer to declare my hash variables as static, because then I can access them without finding a reference to them from other scripts. (A drawback is, that Unity’s debug inspector does not show static fields, so you have to set breakpoints or print to the console, if you want to debug your strings and hashes.)

If you’d like to use auto-completion on string parameters to easily remember them and prevent spelling-errors, try this:

Note, that a class which only stores variables doesn’t need to be attached to a GameObject. You can also declare multiple classes within one file.

"Auto-completion" for string values through static class.
“Auto-completion” for string values through static class.

 

Recap: Use hash ID’s for Animator layers and parameters. Store strings that will not change in a static class.

MonoDevelop Basics

In this article, you will build a C# script from scratch and learn the most important tools in MonoDevelop.

Code completion

Also referred to as autocomplete or IntelliSense, code completion describes the greatest tool every created for programmers. By its name you alreasy know what it does, but we’ll go over the details soon.

First, open a Unity project and create a new C# script called Player. Next, open it in MonoDevelop by double clicking it and delete everything inside it. Now, let’s start from scratch:

This is all you need to define a class in C#. But before you start typing, follow these guidelines:

MonoDevelop code-completion.
MonoDevelop code-completion.

Type the first two letters of the word public and hit Enter to auto-complete it. From now on, there will be almost nothing that you have to type out in its full length. If auto-completion doesn’t pop up you can force it via a keyboard shortcut; in my case: CTRL + Space.

If you’re having trouble, check your preferences:

MonoDevelop code completion preferences.
MonoDevelop code completion preferences.

 

Continue to define your class by typing the letter and hitting Enter to autocomplete it to class. Names like Player are not (yet) in your auto complete list, so you’ll have to spell it out. But once you’re done with your class definition, you will also have access to Player in your code completion window.

In fact, auto completion is a list of all keywords that are currently available to you. This includes keywords from the C# language itself, such as public or class, as well as classes or variables, which you defined.

Next, we want our script to derive from the MonoBehaviour class, because this is what we need to make our script work as a component on a GameObject in Unity. If you try to type out the keyword, there won’t be any auto complete suggestions at the moment. This is, because MonoDevelop doesn’t know about Unity’s MonoBehaviour at the moment. Let’s tell it:

Remember to only type the first few letters and then autocomplete with Enter.

UnityEngine is a collection of classes. MonoBehaviour is one of the classes that live within the UnityEngine namespace. Now, we can use auto completion to inherit from MonoBehaviour and give our class access to all the Unity goodies.

Now we have a functioning class. Save your file and check for errors in Unity, by attaching your script to a GameObject in the scene and hitting Play. Open the console window to see, if any errors appear in the log.

Continue with the following example:

For testing purposes, we will define another class within the same C# script. In fact, this is not a unusual thing to do, just be aware of the fact, that only the first class in a file can be a MonoBehaviour. Following MonoBehaviours will just not work correctly, although no error is shown in Unity.

Construct a new instance of the Test class in the Start() method and store a reference to it in a variable called test of the Type Test.

If this does not yet make sense, don’t worry, just copy the code and follow along to learn more about auto completion.

Code completion showing class members.
Code completion showing class members.

 

Now that we a class instance called test, we can use the dot-operator to access members (public variables) of that class.

Use auto completion to find your variables.

A short rundown of icons used in MonoDevelop’s auto completion:

  • M : method (function)
  • P : property
  • F : field (member variable)
  • C : class

(There are a few more icons, but I’ll save them for later.)

Here is our complete script with a print statement to see if it works correctly:

Checking variable values via the print statement is rather tedious and not very elegant. Click here to learn how to make private variable visible in the inspector.

Renaming variables and classes

If you ever want to rename a variable, be sure to use MonoDevelop’s renaming tool: Select your variable (highlight it or just place your cursor within it) and hit CMD/CTRL + R or right click Refactor Rename. This process is called refactoring because we don’t just want to rename a single word, but instead rename all instances of our variable.

Refactoring a class works the same way, but be aware that a class that derives from MonoBehaviour must have the same name as the file that is attached to a GameObject in the scene.

Shortcuts

Line endings