Update your C # in Unity ~ nameof ~

3 minute read

There was a time when only old C # could be used in Unity. But that’s a thing of the past. The latest LTS at the time of this writing, Unity 2019.4, supports C # 7.3. In addition, C # 8.0 will be supported in Unity 2020.2, the latest Beta version at the time of this writing.

Since I could only use old C # in Unity for a long time, I think there are many people who say “Is there such a function in C #? I didn’t know!”. In this “Update your C # in Unity” series, we will introduce “You can use relatively new features of C # in Unity like this!”.


Language feature name: nameof
Additional version: C # 6.0
Description: Ability to get member names such as variable names, type names, method field properties, etc. as string literals

As a usage example

  • Use nameof instead of string literals where method names are required, such as the first argument of SendMessage.
  • Use nameof instead of string literal in places where field names are required, such as the first argument of FindProperty of SerializedObject.

  • Personally, I think you should avoid using SendMessage. However, if it is unavoidable for some reason, or if you need to make gradual improvements, it is recommended that you first make small improvements with the name of.
using UnityEngine;

public class EnemyLauncher : MonoBehaviour
{
    public void LaunchEnemy()
    {
        Debug.Log("Launched");
    }
}
using UnityEngine;

public class GameController : MonoBehaviour
{
    public void Start()
    {
        SendMessage(nameof(EnemyLauncher.LaunchEnemy));
    }
}

If you take the name of a member (method or field) as a string literal as an argument, like SendMessage or FindProperty, use nameof instead of embedding the string literal directly in your code.

The advantage of using nameof is that you can prevent forgetting to change the corresponding string literal when the type name or member name changes.”

Let’s say you’re passing a string literal as a direct argument and using SendMessage as follows:

using UnityEngine;

public class GameController : MonoBehaviour
{
    public void Start()
    {
        //You should avoid embedding string literals directly like this
        //If the LaunchEnemy method of type EnemyLauncher is renamed, a runtime error will occur.
        SendMessage("LaunchEnemy");
    }
}

At this time, let’s say that the name of the LaunchEnemy method of the EnemyLauncher type is changed. At the same time, you also have to change the string literal " LaunchEnemy " that is embedded directly in the GameController. However, it is possible to inadvertently change this and miss it. In that case, you will get a run-time error. Such run-time errors are very annoying.


On the other hand, what if you used the name of introduced this time as follows?
If you rename the LaunchEnemy method of type EnemyLauncher, you will get a compile error in the nameof in the GameController and you will immediately notice the problem.
You can eliminate the possibility of annoying run-time errors “caused by changing method names”.
Also, if the name of the LaunchEnemy method of the EnemyLauncher type is changed by the rename function of Visual Sturio or Rider, the inside of the nameof is also automatically renamed, and the code can be edited very smoothly.

using UnityEngine;

public class GameController : MonoBehaviour
{
    public void Start()
    {
        //If the LaunchEnemy method of type EnemyLauncher is renamed, you will get a compile error and be aware of the cause of the annoying run-time error in advance.
        //Also, if you use the rename function of the IDE, the contents of ↓ will also be renamed.
        SendMessage(nameof(EnemyLauncher.LaunchEnemy));
    }
}

The advantage of using name of in this way is that you can prevent forgetting to change the corresponding string literal when the type name or member name changes.


In this post, I explained the usage example of name of in Unity and its advantages.
The more common use of nameof in C # is to implement INotifyPropertyChanged or use it as an argument to ArgumentException.
In Unity, I think there are situations where it is used for error texts and warning texts.
Use nameof positively when you need member names such as variable names, type names, method field properties, etc. as string literals.

  • In this post, I introduced name of using SendMessage as an example, but I personally think that the use of SendMessage should be avoided. However, if it is unavoidable for some reason, or if you need to make gradual improvements, it is recommended that you first make small improvements with name of.

Related / Reference