Case Sensitivity in C#

C# is a case-sensitive language. This means that “hello” and “Hello” are considered completely different by the language. This can often be a problem for beginners, and also can cause the occasional head-scratching for advanced C# or Unity developers.

Generally, in C#, the case sensitivity is not a big concern, as the compiler and IDE (the program you write the code with, e.g. Visual Studio Code or Visual Studio) will tell you if you get something wrong, e.g. if you declared a variable called Speed (upper-case S) and then tried to do this:

speed = 1000;

Your IDE will give you an error something like “The name ‘speed’ does not exist in the current context”, and it will be unable to compile and run your code until you fix the error. Your IDE will tell you exactly which line of code the error is on, and which variable or method has the wrong name.

It’s quite easy to learn the habit of using correct capitalisation when you literally can’t run your program with errors.

Case Sensitivity in Unity

The problem for Unity beginners is the way that Unity’s MonoBehaviour class has special methods that Unity runs automatically during the game life cycle.

If your MonoBehaviour script (generally, all your Unity scripts will be MonoBehaviour scripts) has a method called Update(), that method will run automatically every frame. Unity does that for you. If you have a method called Start(), Unity will run that method when your scene starts.

But…if you name your methods update() and start() (with lowercase first letters), Unity will ignore them. As far as it’s concerned they are not Update() and Start(). However, here is the problem for Unity beginners – neither C# nor Unity will consider calling your methods update() and start() (or onTriggerEnter() or awAkE(), etc.) an error, as they are valid method names. But they won’t be run according to Unity’s game life cycle unless they are written exactly as Unity expects them.

Another thing worth remembering is that you can have both Update() and update() in the same script, and the same goes for variables called ‘speed’ and ‘Speed’…but they will be completely separate from each other, so avoid that!

Tips

Make sure your IDE of choice has its integration with Unity set up correctly. Unity has some packages (available from the Unity Package Manager) to connect to Visual Studio and Visual Studio Code, and there are also 3rd party plugins available for different IDEs. These tools and addons will provide help by making Unity’s built-in method names display in a different colour or to have some other kind of hint to distinguish the methods Unity recognises as Unity life cycle methods from all your other code.

Unity names things with quite standard naming conventions, where method names are written in CamelCase where every word in the name starts with a capital letter. If you make sure you do this with your method names, it will be less likely you will name a Unity method incorrectly.

Leave a Comment