Alternative Type Cast

In this lecture, the as keyword is used to attempt to cast the currently set runtime animator controller to an animator override controller. I found that as of C# 7, you can use pattern matching with the is keyword to simplify the code a bit. Included below with a link for more information.

if (weaponOverride != null)
{
    animator.runtimeAnimatorController = weaponOverride;
}
else if (animator.runtimeAnimatorController is AnimatorOverrideController overrideController)
{
    animator.runtimeAnimatorController = overrideController.runtimeAnimatorController;
}

More information: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is

4 Likes

Note that before Unity 2018.3, this will not work, as that’s when they officially introduced support for the C#7 standard. Thinking on it, though, I think that’s when they introduced nested prefabs, so hopefully everybody in the course is already using 2018.3 or greater.

2 Likes

Privacy & Terms